0

I've made Android app that uses offline-mode. Also it has button "Sync", on click - syncronization with server is performed (server is not Firebase-service DB).

I want to do the same sync without this button once a minute when my app is on foreground and when network connection is on.

WorkManager seems the best solution for the usecase, but according to this article minimum interval for WorkManager is 15 minutes.

Other ways described in this article are: ForegroundService, AlarmManager and background Thread.

Also I found solution Sync Adapter

What way would be the most efficient for the case?

P.S. I understand that this scenario is not very clean and probably the best would be add online-mode and switch between two modes without frequent syncs. But I have some reasons at this time not to do that

sergiy tikhonov
  • 4,961
  • 1
  • 10
  • 27

1 Answers1

0

Finally, I chose next way:

  1. To invoke one-minute sync I used ThreadPool. This discussion helped me to choose.
  2. Inside my Application class I put that code:

    val scheduler = Executor.newSingleThreadScheduledExecutor()
    scheduler.scheduleAtFixedRate({
        .... <My Sync Block> ....
    }, 0, 1, TimeUnit.MINUTES)
    
  3. To prevent getting the same data from the server (there could be huge pieces of data in my case), I had to use MD-5 alghoritm on the server-side. It works as follows:

    • server emits data with hashes (for each piece od data)
    • mobile app gets data and saved both data and hashes in SQLite. In next sync app sends hashes back in request
    • server looks if requested data has different hashes and includes only updated data in respond
sergiy tikhonov
  • 4,961
  • 1
  • 10
  • 27