我是一个Android开发的初学者。我对与 rxjava2
, MVVM
, retrofit2
变成http请求.有了 MVVM
arch,我构建了以下请求。
在这个片段中,我使用嵌套的 livedata
观察者。它确保api请求以相应的顺序执行。但我想改进这一点。我想把多个请求做成异步的,而且可变的livedata可以一个一个的发布或设置值。我应该如何完成呢?我知道 rxjava2
用zip等一些功能可以异步发送请求,但如何将值发布到UI线程。我不想让观察者嵌套.谢谢。
private fun callAPIProcess(){
showProgressDialog(ctx)
viewModel?.callGetTeammateListAPI()?.observe(this, Observer {
if(it?.status!!.equals("Y")){
initRightBtn()
}
else{
dismissProgressDialog()
MyDialog(ctx, true, { viewModel!!::callGetTeammateListAPI }).createNativeNetworkDialog()
}
viewModel?.callGetTeamListAPI()?.observe(this, Observer {
if(it?.status!!.equals("Y")){
initMemberList()
}
else{
dismissProgressDialog()
MyDialog(ctx, true, { viewModel!!::callGetTeamListAPI }).createNativeNetworkDialog()
}
viewModel?.callGetMessageSumAPI2()?.observe(this, Observer {tabBarButton->
if(tabBarButton?.status!!.equals("Y")){
tabBarButton.ownMessageNum?.let { newCallMemberAdapter?.setOwn(it) }
setTitle(tabBarButton?.headerTitleText)
updateTabNum()
}
else{
dismissProgressDialog()
MyDialog(ctx, true, { viewModel!!::callGetMessageSumAPI2 }).createNativeNetworkDialog()
}
viewModel?.callGetCall(ryetvSearchBar.text.toString())?.observe(this, Observer {
it.let {
adapter?.setData(it)
adapter?.notifyDataSetChanged() }
Log.d("CallFragment_debug","callgetcall_")
dismissProgressDialog()
})
})
})
})
}
fun callGetTeammateListAPI():LiveData<GetTeammateListModel> {
getTeammateListModelData = MutableLiveData()
repo.callGetTeammateList("0",
MyObserver(object : ObserverListener<GetTeammateListModel> {
override fun onNext(t: GetTeammateListModel) {
if (t.status.equals("Y")) {
BaseHelper.teammateModel = t.teammates
}
Log.d("CallViewModel_Debug","getTeammateListModelData")
getTeammateListModelData?.postValue(t)
}
override fun onError() {
dismissProgressDialog()
}
},true)
)
return getTeammateListModelData!!
}
fun callGetTeamListAPI():LiveData<GetTeamListModel> {
getTeamListModelData = MutableLiveData()
repo.callGetTeamList(MyObserver(object : ObserverListener<GetTeamListModel> {
override fun onNext(g: GetTeamListModel) {
if (g.status.equals("Y")) {
BaseHelper.teamsList = g.teams
}
Log.d("CallViewModel_Debug","getTeamListModelData")
getTeamListModelData?.postValue(g)
}
override fun onError() {
dismissProgressDialog()
}
},true))
return getTeamListModelData!!
}