[英]How can I ensure the first Observable is subscribed before the second one emits in RxJava2?
Having read the article MODEL-VIEW-INTENT written by Hannes Dorfmann, I designed my app using the MVI pattern. 阅读了Hannes Dorfmann撰写的MODEL-VIEW-INTENT文章后,我使用MVI模式设计了我的应用程序。
This is my design chart of my application. 这是我的应用程序设计图。
I have two reusable fragments, BarFragment and ContentFragment. 我有两个可重用的片段,BarFragment和ContentFragment。 And my activity is made up of those two fragments.
我的活动由这两个片段组成。
The data flow is: 数据流为:
Step 1: The changeKeywordIntent() method gathers user's input from the search view in BarFragment. 步骤1:changeKeywordIntent()方法从BarFragment的搜索视图中收集用户的输入。
override fun changeKeywordIntent(): Observable<String?> {
return RxSearchView.queryTextChangeEvents(view!!.search)
.filter { it.isSubmitted }
.map { it.queryText().toString() }
}
Step 2: To reuse the fragment, the intent is not subscribed by render() method directly. 步骤2:要重用该片段,render()方法不会直接订阅该意图。
On receiving the intent, the presenter emit an PartialViewState.ChangeKeyword(keyword) object to the partialViewStateSubject first. 收到此意图后,演示者首先将PartialViewState.ChangeKeyword(keyword)对象发送给partialViewStateSubject。 This partial view state will be reduced to ViewState and then, consumed by all the MvpView's render() method.
此部分视图状态将被还原为ViewState,然后由所有MvpView的render()方法使用。 IE, the views will show that keyword at a proper position.
IE,视图将在适当位置显示该关键字。
This is like a broadcast. 这就像广播一样。
After the PartialViewState.ChangeKeyword(keyword) object has been rendered, the presenter calls a business logic and then emit a PartialViewState.ContentFirstPage(response.body()) start with a PartialViewState.Loading() object. 呈现PartialViewState.ChangeKeyword(keyword)对象之后,演示者调用业务逻辑,然后发出从PartialViewState.Loading()对象开始的PartialViewState.ContentFirstPage(response.body())。
My question is in this step, Call which RxJava2 method can I ensure that before the second observable emit, the first observable is subscribed? 我的问题是在此步骤中,可以调用哪个RxJava2方法来确保在第二个可观察对象发出之前,第一个可观察对象已预订? And which method to create the second observable?
哪种方法可以创建第二个可观察的对象?
intent(EmployeesBarView::changeKeywordIntent).flatMap { keyword ->
Observable.concat(
Observable.just(EmployeesScenarioPartialViewState.ChangeKeyword(keyword) as EmployeesScenarioPartialViewState),
Observable.someMethod {
// to ensure viewStateSubject.value is the value after PartialViewState.ChangeKeyword(keyword) has been subscribed, I don't know which method I should call
Observable.just(viewStateSubject.value).flatMap { viewState ->
EmployeeStub.getInstance(activity.baseContext).query(toFilterLogicExpr(viewState.keyword), toOrderByListExpr(viewState.orderBy), toRangeExpr(0, viewState.pageSize), null).toObservable()
.map { response -> EmployeesScenarioPartialViewState.ContentFirstPage(response.body()) as EmployeesScenarioPartialViewState }
.startWith(EmployeesScenarioPartialViewState.Loading())
.onErrorReturn { error -> EmployeesScenarioPartialViewState.Error(error.message) }
}
})
}.subscribe(partialViewStateSubject::onNext).addTo(compositeDisposable)
Step 3: When a PartialViewState object is received, reduce it to a ViewState object and push the ViewState object to viewStateSubject. 步骤3:收到PartialViewState对象时,将其简化为ViewState对象,然后将ViewState对象推送到viewStateSubject。 Key code is
关键代码是
employeesScenarioViewStatePartialSubject
.scan(initialEmployeesScenarioViewState(), ::reduceEmployeesScenarioViewState)
.subscribe(employeesScenarioViewStateSubject::onNext)
.addTo(compositeDisposable)
Step 4: All presenters subscribe viewStateSubject in bindIntents() method 步骤4:所有演示者都通过bindIntents()方法订阅viewStateSubject
subscribeViewState(viewStateSubject.observeOn(AndroidSchedulers.mainThread())) { obj, state -> obj.render(state) }
Step 5: render the viewState object, the code could be skipped in this question. 步骤5:呈现viewState对象,此问题中的代码可以跳过。
So my question is in step 2, could anyone help me with that code with RxJava2? 所以我的问题是在步骤2中,有人可以在RxJava2上帮助我处理该代码吗? Thank you.
谢谢。
Use : 采用 :
.flatMap{
Observable.fromCallable{events.viewState}
.flatMap{viewState ->......}
}.subscribe(partialViewStateSubject::onNext).addTo(compositeDisposable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.