I have an Android app which stores data in a file system. This file system synchronizes with a server when there is an internet connection available (but is designed to work without a network connection if necessary). The data is also kept in ram for fast access. See the diagram below:

enter image description here

The general idea is that app Activities access the LiveData singletons which then by some process gets synchronized with the file cache (json files).

My question is regarding the question mark in the diagram. I'm not really sure what the best pattern would be for doing this synchronization.

The JobService will send broadcasts whenever data is changed from its end so this can be used to initialize a sync. Similarly the LiveData class has its observe pattern which can be used for changes from that direction.

The question is what method to use to do this sync. I have some ideas

  • The LiveData singletons handle it themselves; a thread is started and runs the sync whenever there is a change (but I don't know how they react if they go out of scope with an active broadcast receiver and no way to unregister it).
  • A second service which just lives to listen to broadcasts from the JobService and changes in the LiveData singletons and update accordingly.
  • Extend the Application class to handle the synchronization from there. Its lifetime should at least guarantee there are no problems with broadcasts to unloaded classes.

What would be best? Any better alternatives?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.