问题
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