Does take(1) stop the upstream flowable doing work?

主宰稳场 提交于 2020-01-16 09:41:10

问题


Let's assume we have some Flowable that looks like:

val exampleFlowable = Flowable.create<Int>({ emitter ->
    while (true) {
        Thread.sleep(TimeUnit.SECONDS.toMillis(1))
        emitter.onNext(1)
    }
}, BackpressureStrategy.LATEST)
        .subscribeOn(Schedulers.io())

And then I call take(1) on it and subscribe to it like so (notice, disposing of the disposable in the subscribe block):

var disposable: Disposable? = null

disposable = exampleFlowable.take(1)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({ first ->
            disposable?.dispose()
        })

Will the exampleFlowable stop doing work when the value is consumed in the subscribe block?

How about if I didn't dispose in the subscribe block? Would exampleFlowable still continue to do work then?

Also, what if I called singleOrError after calling take(1) and didn't dispose in the subscribe block? Would the exampleFlowable continue to do work in this case, or does converting the stream to a Single mean the upstream will only do work until a single value OR error is emitted? For instance:

   exampleFlowable.take(1).singleOrError()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({ single ->

            })

来源:https://stackoverflow.com/questions/59752373/does-take1-stop-the-upstream-flowable-doing-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!