Android Rxjava2/retrofit2 Chaining Calls With Pagination Token
I'm using a REST API to query for a list of Person objects. Max limit is 100 people in response. I need to fetch all people, and the total amount is unknown. There is a field in th
Solution 1:
Something similar to this would work (a bit generalized):
public Observable<Response> paginate(String initialUrl){
AtomicReference<String> url = new AtomicReference<>(initialUrl)
return Observable.defer(() -> api.loadUsers(url.get())
.doOnNext(response -> url.set(response.next))
.repeatWhen(r -> r.takeWhile(!url.get().isEmpty()));
}
Solution 2:
You can do something like this.
Observable.just("input as one item if any").
.map(new Function<String, List<Person>>(){
@Override
public List<Person> apply(String inPut) throws Exception {
// using inPut, get url, service name, and other input
// params
String nextUrl = "firsturl";
List<Person> persons = new ArrayList<Persons>();
while(nextUrl != null){
//call service using plain retrofit passing nextUrl and get
//person objects
//add 100 person objects from each call
persons.add();
//get next Url
if(nextUrlFromResponse != null){
nextUrl = "next url from previous call";
}else{
nextUrl = null;
}
}
return persons;
}
}).subscribeOn(Schedulers.io()).observeOn(Androidmainthread);
Post a Comment for "Android Rxjava2/retrofit2 Chaining Calls With Pagination Token"