38

I'm a little bit confused about the order you can call the subscribeOn and observeOn methods on observables. I read a couple of posts and one guys says that it doesn't matter and just uses thing in his example and other people say it does matter. So here is my question:

For example:

self.remoteService.rxGetAllLanguages()
            .observeOn(MainScheduler.instance)
            .subscribeOn(ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background))
            .subscribe({ e in
                switch e {
                case .Next(let element):

                case .Error(let e):
                    DDLogError("Error in  \(e)")
                case .Completed:
                    DDLogDebug("Completed")
                }
                }
            ).addDisposableTo(self.disposeBag)

Is that the same as:

  self.remoteService.rxGetAllLanguages()
                    .subscribeOn(ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background))
                    .observeOn(MainScheduler.instance)
                    .subscribe({ e in
                        switch e {
                        case .Next(let element):

                        case .Error(let e):
                            DDLogError("Error in  \(e)")
                        case .Completed:
                            DDLogDebug("Completed")
                        }
                        }
                    ).addDisposableTo(self.disposeBag)

If I correctly understand the mechanisms they are different. The first one does all the work on the main thread and the second does all the work on another thread and then dispatches back to the main thread. But I'm nut sure so can someone clear this for me please?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
user1007522
  • 7,858
  • 17
  • 69
  • 113

3 Answers3

88

Where you call subscribeOn() in a chain doesn't really matter. Where you call observeOn() does matter.

subscribeOn() tells the whole chain which thread to start processing on. You should only call it once per chain. If you call it again lower down the stream it will have no effect.

observeOn() causes all operations which happen below it to be executed on the specified scheduler. You can call it multiple times per stream to move between different threads.

Take the following example:

doSomethingRx()
    .subscribeOn(BackgroundScheduler)
    .doAnotherThing()
    .observeOn(ComputationScheduler)
    .doSomethingElse()
    .observeOn(MainScheduler)
    .subscribe(//...)
  • The subscribeOn causes doSomethingRx to be called on the BackgroundScheduler.
  • doAnotherThing will continue on BackgroundScheduler
  • then observeOn switches the stream to the ComputationScheduler
  • doSomethingElse will happen on the ComputationScheduler
  • another observeOn switches the stream to the MainScheduler
  • subscribe happens on the MainScheduler
Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
Jahnold
  • 7,623
  • 2
  • 37
  • 31
  • Hi @Jahnold, the RxSwift Documentation says - "If you want to start sequence generation (subscribe method) and call dispose on a specific scheduler, use subscribeOn(scheduler)." Does this mean, if we specify subscribeOn(:), it'll will start the chain on specified on the specified thread, also the dispose of the disposable will be called on the same specified thread? – iamyogish Feb 02 '17 at 09:18
3

Yea you're correct. observeOn will only receive the events on the thread you've specified, whereas subscribeOn will actually execute the work within the specified thread.

solidcell
  • 7,639
  • 4
  • 40
  • 59
2

.subscribeOn() operator affect on which sheduler chain will be created (to the left of it) and it work once. .observeOn() influence operator to the right of it - on which schedulers data will be processed after operator.

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11