How to perform RxSwift in the background?
Asked Answered
C

1

5

I am trying to perform a complex computation in the background upon table row selection but it freezes the UI. Please have a look at my code and tell me what might be wrong.

tableView.rx
    .modelSelected(Sring.self)
    .flatMap { item -> Observable<String> in
        
        for _ in 1...2_500 {
            for _ in 1...1_000 {
                
            }
        }
        return Observable.just("Hello world!")

    }
    .subscribeOn(ConcurrentDispatchQueueScheduler(qos: .background))
    .observeOn(MainScheduler.instance)
    .retry()
    .subscribe(onNext: { value in
        print(value)
    })
    .disposed(by: bag)
Crites answered 2/12, 2020 at 14:16 Comment(0)
M
8

The first thing to understand with this code is that subscribeOn determines which thread the event generator of the observable will be called on, not which thread the the values will be emitted on. If the source Observable emits on the same thread it's subscribed on, then your code would have worked (for example, if you were using Observable.just(_:). But modelSelected emits on the main thread, regardless on what thread it was subscribed on. The flatMap operator calls its closure on the thread that its source emitted on so that also is on the main thread.

The upshot of all of this is that it's rarely useful to call subscribeOn(_:). The only time it actually does what you would expect is if the ultimate source (the one most upstream) Observable's generator is a synchronous, blocking, function. (Which isn't the case with tableView.rx.modelSelected(String.self))

What you want in this case is something more like:

tableView.rx.modelSelected(String.self)
    .observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
    .flatMap { item -> Observable<String> in
        sleep(3)
        return Observable.just("Hello world!")
    }
    .retry()
    .observeOn(MainScheduler.instance)
    .subscribe(onNext: { value in
        print(value)
    })
    .disposed(by: bag)
Modular answered 3/12, 2020 at 2:24 Comment(1)
Thank you very much for your kind explanation. Everything works just like in your code above.Crites

© 2022 - 2024 — McMap. All rights reserved.