订阅和观察的顺序重要吗? [英] Does the order of subscribeOn and observeOn matter?

查看:117
本文介绍了订阅和观察的顺序重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在可观察对象上调用subscribeOnobserveOn方法的顺序,我有些困惑.我读了几篇文章,一个人说这没关系,只是在他的例子中使用了东西,其他人说这没关系.所以这是我的问题:

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:

例如:

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)

与以下相同:

  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?

推荐答案

在链中调用subscribeOn()的位置并不重要.呼叫observeOn()的位置很重要.

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

subscribeOn()告诉整个链开始在哪个线程上进行处理.每个链只应调用一次.如果在流的下方再次调用它,则将无效.

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()导致在其下发生的所有操作都在指定的调度程序上执行.您可以在每个流中多次调用它,以在不同线程之间移动.

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.

以下面的示例为例:

doSomethingRx()
    .subscribeOn(BackgroundScheduler)
    .doAnotherThing()
    .observeOn(ComputationScheduler)
    .doSomethingElse()
    .observeOn(MainScheduler)
    .subscribe(//...)

  • subscribeOn导致doSomethingRx在BackgroundScheduler上被调用.
  • doAnotherThing将在BackgroundScheduler上继续
  • 然后observeOn将流切换到ComputationScheduler
  • doSomethingElse将在ComputationScheduler上发生
  • 另一个observeOn将流切换到MainScheduler
  • 订阅发生在MainScheduler上
    • 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
    • 这篇关于订阅和观察的顺序重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆