0
votes

I have a scenario where I need to call search api to get a list of particular item based on the search radius on map. The requirement is to show at least five results. In my case I have two Apis (two observables). First I need to call getExpandedSearchRadius() to get radius and perform second call, doSearch() with radius as parameter. Suppose the doSearchApi Call returns only 2 results I need to repeat the both api calls till I get the minimum result of 5. On each repeat getExpandedSearchRadius need to call to return a new expanded radius and perform doSearch with new radius.

Here the problem is each time the repeat() is called getExpandedSearchRadius Api is not executing, only the second call is executing with the initial radius, resulting in same searchResponse. below is a sample that I tried.

getExpandedSearchRadius().flatMap{ radius -> doSearch(radius)}
                      .repeat()
                      .takeUntil(searchResponse.getItems().size >=5)
                      .map(anotherClass::displayOnMap)
1
How is getExpandedSearchRadius implemented. Did you by any chance use just? - akarnokd
getExpandedSearchRadius() returns Observable<radius> which emits a single item. The first time it calls in a session ,it will be 6 Miles. Second call to same increase to 20 Miles. Each time it calls the radius get increased. - linliz
I meant, post the implementation of getExpandedSearchRadius. - akarnokd

1 Answers

1
votes

You can use Observable.defer(), as it acts as an Observable factory, then, when repeat operator takes effect, builder factory .defer() creates another fresh observable and will invoke getExpandedSearchRadius() also.

See doc: (Defer Operator)

Example Code:

Observable.defer(() -> getExpandedSearchRadius())
        .flatMap{ radius -> doSearch(radius)}
                  .repeat()
                  .takeUntil(searchResponse.getItems().size >=5)
                  .map(anotherClass::displayOnMap)