paxcow
paxcow

Reputation: 1941

Periodically sending the same request with RxJava2

So, I have an API call which returns a list of Dog breeds, and another call that takes the dogBreedIds and fetches dog names for each of these breed IDs. In onNext, I add these dog names into a list, and in the doOnComplete, I display these in a recycler view.

Both getDogBreeds and getDogNames return an Observable<List> is an example of rx chain i accomplish this with:

petsRepository.getDogBreeds()
            .map { breeds ->
                breeds.items.map {
                    it.id
                }
            }
            .flatMapIterable { listOfIds -> listOfIds }
            .flatMap { dogId -> getDogNames(dogId) }
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnComplete { displayDogNames() }
            .subscribe(this::onDogNamesLoaded, this::onError)

What I'm trying to do is, to re-trigger this whole process once in every 60 seconds, and then, compare the response list with the first response I have and display it to the user if the response list has differences (this part is perhaps irrelevant)

I have tried to use Observable.interval(), however I could not succeed in doing this. I have checked plenty of examples with no success.

Please let me know if I should be providing more information on this. Thanks!

Upvotes: 1

Views: 66

Answers (1)

r2rek
r2rek

Reputation: 2233

You could go for interval like this:

Observable.interval(0, 60, TimeUnit.SECONDS)
                .flatMap {
                    petsRepository.getDogBreeds()
                }.map {...}

The rest should remain pretty much the same.

As for the displaying part - I'm going to assume you're using RecyclerView(correct me if not). If so, then use ListAdapter, DiffUtils will handle displaying differences in lists.

Hope this helps!

Upvotes: 3

Related Questions