代码之家  ›  专栏  ›  技术社区  ›  Max

在flatMapCompletable中调用subscribeOn时将使用什么线程?

  •  0
  • Max  · 技术社区  · 6 年前

    我有一个复杂的,我创造这样:

    public class Params {
        // Contains input parameters
    }
    
    public Completable startSomething(Params params) {
        return Completable
            .fromRunnable(() -> someMainThreadThing(params))
            .subscribeOn(this.schedulers.mainThread());
    }
    

    现在,我有消息说 Single<Params>

    public Completable doSomethingWithParams(Single<Params> params) {
        // Do something with the params
    }
    

    我想实施 doSomethingWithParams 就像这样:

    public Completable doSomethingWithParams(Single<Params> params) {
        return params.flatMapCompletable(::startSomething);
    }
    

    我的问题是 someMainThreadThing 保证在主线程上执行,或者 影响执行它的线程?

    1 回复  |  直到 6 年前
        1
  •  0
  •   ConstOrVar    6 年前

    根据文件 Single.flatMapCompletable()

    默认情况下,flatMapCompletable不在特定计划程序上运行。

    completable 具有 startSomething() 如果不调用调度程序之间的显式切换,则将在与以前的链操作相同的调度程序(线程)上调用。

    在可以使用的调度程序之间切换 observeOn()

    Single.fromCallable(getParams())
        .subscribeOn(Schedulers.io())
        .observeOn(this.schedulers.mainThread())
        .flatMapCompletable(::startSomething)
    

    那样的话 subscribeOn() 使执行 getParams() Schedulers.io() ,之后 切换到主线程并执行 开始某事()