Question:
Initial data :
There is a project that uses methods from the SDK of a third-party project (asynchronous methods for working with their server with a callback parameter) and contains Retrofit2 + OkHttp + Rx for working with the server directly. For convenience, lambdas are used.
Task :
It is necessary that all requests (both from the SDK and via Retrofit) are executed no more than 5 times per second, and if the limit is exceeded, they are executed with a delay.
Question :
How to implement it? The first thing that comes to mind is Service + BroadcastReceiver. But you will have to listen to the receiver in each activity/fragment, plus you will get a not so convenient implementation of callbacks… Maybe you can somehow implement this using Rx (including wrapping SDK methods) to leave lambdas for convenience?
Answer:
Maybe this will help?
Subscription subscription = Observable.interval(1000, 5000,
TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
public void call(Long aLong) {
// here is the task that should repeat
}
});
https://stackoverflow.com/a/49718536/10965132