10

I'm using Retrofit in combination with rx-java and have the following question:

Is there a way to queue API calls to a server when the device is offline so that they can be fired when the device is back online again.

Ben Groot
  • 5,040
  • 3
  • 40
  • 47

2 Answers2

13

EDIT: March 2021 you should now use Work Manager instead.


You need to add queue to your project in the first place and pipe Retrofit jobs thru that queue. So either create own queue or check if libraries like android-priority-jobqueue fit your requirements (that one also handles persistency of queued jobs, so app kill/reboot is less painful)

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thanks! Is there a way to save the queue to a persistence storage for when the app is restarted? – Ben Groot Aug 07 '15 at 08:01
  • 1
    Sure. Actually android-priority-jobqueue does it out of the box (in fact it stores it first, then processes). With custom implementation you have to do that yourself – Marcin Orlowski Aug 07 '15 at 08:29
  • Wow that sounds amazing! I'll dive into it! Thanks! – Ben Groot Aug 07 '15 at 09:22
  • 3
    I've implemented the lib in my code and I have to say that I'm really amazed! This is by far the best approach I could imaging. Many thanks for pointing me into the right direction! – Ben Groot Aug 18 '15 at 13:17
1

I think Marcin Orlowski's answer was good. But if you want to go with rxjava, you can use .retry() with check, are you online, inside of it.

rxjava: Can I use retry() but with delay?

Community
  • 1
  • 1
Stas Parshin
  • 7,973
  • 3
  • 24
  • 43
  • Thanks for your answer. The calls should also be cached and sent when the app is restarted after a day or so. The retry function in only when the app is in memory right? – Ben Groot Aug 07 '15 at 08:00