I have a big list of strings that needs to be checked against remote API.
Observable.from(List<String> strings) // let's say the `strings` has > 5000 items
.buffer(50) // splitting the strings into 50-sized chunks, it returns Observable<List<String>> (fast)
.flatMap((strings) -> {
// checkPhoneNumbers is a network call using Retrofit's RxJava (slow)
return mSyncApi.checkPhoneNumbers(strings);
})
.reduce( ... ) // aggregate all checking results
.subscribe( ... );
The problem is buffer()
seems to emit List<String>
too fast that all of multiple .checkPhoneNumbers()
get executed almost in the same time. What I would like to achieve is to enqueue .checkPhoneNumbers()
to better support devices with slow connection.
Throttling the emitted List<String>
by predefined time interval doesn't make sense since it will be a disadvantage for devices with lightning fast connection.
I have tried RxJava's serialize()
right after the flatMap()
but it doesn't seems make any difference (although I don't know if it's the right use of serialize
).
Any alternative approaches appreciated! Thanks.