RxSwift — background task without freezing UI RxSwift — background task without freezing UI multithreading multithreading

RxSwift — background task without freezing UI


I believe the problem lies in your implementation of .heavyLiftingFuncReturningObservable(), namely in the fact that apparently it starts working on current thread instead of waiting until being subscribed to and running on background scheduler. Solution for that is to use .deferred() inside of .heavyLiftingFuncReturningObservable() function.

See http://adamborek.com/top-7-rxswift-mistakes/


You can do this:

self.someDisposable = Observable.just(0) // dummy    .observeOn(ConcurrentDispatchQueueScheduler(qos: .background))    .flatMap { [unowned self] _ in        heavyLiftingFuncReturningObservable()    }    .observeOn(MainScheduler.instance)    .subscribe(        onNext: { [weak self] image in            // update UI        },        onError: { ... }    )


Don't do this... use the Observable.deferred { } method

Put the heavy task inside the block and it will run on subscription.

If the work has a single response, use a Single observable type.
If the work only has to complete, use a Completable observable type

Use the observeOn / subscribleOn operators to control the thread doing the work.

Best of all, don't use RX for this use case.