Baseline :

There is a project that uses methods from the third-party project SDK (asynchronous methods for working with their server with a callback parameter) and contains Retrofit2 + OkHttp + Rx for working directly with the server. For convenience, used lambda.

Task :

It is necessary that all requests (from both the SDK and 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 activit / fragment, plus you will get a less convenient implementation of callbacks ... Maybe you can somehow implement this with Rx (including wrapping SDK methods) to leave the lambdas for convenience?

  • It would also be interesting to know the solution. I hope to draw attention to this topic. - Julia
  • You want to synchronize asynchronous methods (at 1/5 second intervals). It is necessary to implement something like an intermediate timer. - bigov September
  • Also faced with a similar task. Did you find a solution? - Anert
  • It would be interesting to know the decision on this topic. If someone has already decided, please give an answer. - Genadij Smehov pm

1 answer 1

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

    Protected by community spirit ♦ Jan 28 at 1:36 .

    Thank you for your interest in this issue. Since he collected a large number of low-quality and spam responses, which had to be deleted, now it’s necessary to have 10 reputation points on the site (the bonus for account association is not counted ).

    Maybe you want to answer one of the unanswered questions ?