-->

How to perform multiple API call synchronously or

2020-05-01 08:39发布

问题:

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