dimanche 22 mai 2016

Pattern for loading/storing/changing/caching data

I am developing an android app where I want to load data in the form of a list with data entries from a backend. Operations that should be possible are: loading a fresh list from the backend, storing it on the disk after it is loaded, caching the list for quick delivery and also being able to change data entries that then should be updated in the cached and stored list and eventually be updated on the backend.

Also the data should survive configuration changes and other activity lifecycles. And if possible the data should be available in a global manner, so that synchronized data is accesed by every requesting instance, maybe by using sigleton or extending application(but i don't want a discussion which of both is better here. I think there is enough about this topic on the internet)

I think this mechanism exist in a lot of famous apps where you can for example get a new list of entries from a server by pressing refreshing and also change list entries e.g. giving a like or dislike etc. that are then updated in your list on the phone and also on the serverside.

However I'm a little bit stuck with what the usual approach to this usually is. What pattern or mixture of patterns is usually used to implement this whole mechanism in a good and clean way?

My approach right now is that i use a Loader to load the data from the backend or file into its cache. That data is then delivered to a fragment which then passes it to other fragments etc.

This approach however has it's limitations, for example if a fragment changes a list entry i cannot update the data in the loaders cache( i probably could get access to it but just in a messy way where I directly need to interact with the loader). Also each fragment would then need to handle the updates of the list in the file system and on the backend by itself. I would prefer this to happen in a single global instance that handles the list.

Also I am not sure what would be the best approach to asynchronous load the data from the backend and file system and also update it in this case. With a Loader? Service? AsyncTask? Handler?

So all in all I am asking which pattern or architecture/structure should be used to implement this in a good and clean way and which Android components suit best for its implementation. (I am not asking for implementation details of single components)

Aucun commentaire:

Enregistrer un commentaire