How to perform multiple API call synchronously or call API sequentially using RxAndroid or RxJava

与世无争的帅哥 提交于 2019-12-02 19:21:11

问题


Hi There I am new in RxJava I am trying to call two API sequential the result of one api used as input to other and lastly return value of second app

Currently I am trying with below code

 public Single<List<LocationParcelableItem>> getUpdateRecentLocation(
      LocationViewType locationViewType, Map<String, String[]> filter) {

    return locationRepository
        .getRecentLocations(filter)
        .subscribeOn(Schedulers.io())
        .flatMapMaybe(this::updateRecentLocationList)
        .flatMapSingle(userLocationParcelableItem -> getRecentLocations(locationViewType));
  }

scenario is like I want updated location always even it is stored in local DB below are the steps that I want to perform

  1. getSavedRecord from Database( locationRepository .getRecentLocations(filter) )

    • return Single<List<RecentLocationRecord>>
  2. collect Ids from above list then call API to get Updated List (.flatMapMaybe(this::updateRecentLocationList))

  3. if record found from step 1 then update database record if not then delete database record
  4. get updated record from Database (same as step 1 - flatMapSingle(userLocationParcelableItem -> getRecentLocations(locationViewType)))

below is working well just thing is when compiler comes to .flatMapMaybe(this::updateRecentLocationList) it doesn't execute code inside method of updateRecentLocationList it directly jump to next flat map as flatMapSingle and same happen there then return without waiting for result. After getUpdateRecentLocation return then updateRecentLocationList is executed

I want flatMap wait for other flatmap to complete and return the result of last faltMap

来源:https://stackoverflow.com/questions/58994906/how-to-perform-multiple-api-call-synchronously-or-call-api-sequentially-using-rx

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