alexsullivan114
11/02/2019, 8:33 PMinit
of a view model of mine may look like this:
init {
myObservable
.filter { whatever() }
.map { whatever() }
.subscribe { pushToMySubject() }
myOtherObservable
.filter { whatever() }
.map { whatever() }
.subscribe { pushToMyOtherSubject() }
myThirdObservable
.filter { whatever() }
.map { whatever() }
.subscribe { pushToMyThirdSubject() }
}
But when using flow, we can only call collect
(which if I'm not mistaken is analagous to subscribe
in RxJava) from within a coroutine context, which makes sense. But, as far as I can tell, calling collect
blocks that coroutine context (my terminology might be garbage here) so you can't really have the above setup because we wouldn't execute the last two subscribes until the first observable completes.
So two questions:
1. Is my understanding correct?
2. What's the preferred path to handle this sort of scenario?octylFractal
11/02/2019, 8:38 PMinit
?alexsullivan114
11/02/2019, 8:38 PMoctylFractal
11/02/2019, 8:39 PMviewModelScope.launch
alexsullivan114
11/02/2019, 8:39 PMoctylFractal
11/02/2019, 8:39 PMlaunch
for each thing you want to collectalexsullivan114
11/02/2019, 8:39 PMlaunch
octylFractal
11/02/2019, 8:39 PMalexsullivan114
11/02/2019, 8:39 PMDominaezzz
11/02/2019, 8:49 PM.launchIn(viewModelScope)
on the flow itself.alexsullivan114
11/02/2019, 10:24 PMelizarov
11/03/2019, 2:29 PMalexsullivan114
11/03/2019, 2:41 PMlaunchIn
you really need to use onEach
or some other side effect operator to actually process your items. Interesting.elizarov
11/03/2019, 2:43 PMcompositeDisposable.add(..... .subscribe { ... })
you do
.... .onEach { ... }.launchIn(scope)
You use scope
instead of compositeDisposable
to track subscriptions and make sure they are closed.