I'm trying to figure out the best strategy for data caching in Android, particularly in view of using the data binding library.
The Android documentation really seems to put a lot of emphasis on using the built-in SQLite database and to access this with the help of content providers. However, assuming that my app retrieves data in JSON format from a REST API, using this strategy would entail the following:
Make HTTP request --> Parse JSON --> Insert results into DB
--> Call content provider --> Build model from cursor --> Bind to view
Not only does this seem like a very roundabout way of doing something relatively simple, but assuming all this happens when the user first opens the app the result would be lots and lots of waiting before anything useful appears on the screen.
To speed things up I may then decide to create my model earlier and let the caching take place in a separate thread, like this:
Make HTTP request --> Build model from JSON --> Bind to view
-->(NEW THREAD) --> Insert results into DB
Once the data is cached, the next time the user opens the app the following would take place:
Call content provider --> Build model from cursor --> Bind to view
But of course this would add even more complexity, for example forcing me to maintain code to build the model from two sources: JSON and the cursors returned by the content provider.
Given the above I am even more tempted to do away with the SQLite/ContentProvider model and instead do the following:
Make HTTP request --> Build Model and Store JSON to file --> Bind to view
But while this would greatly reduce boilerplate, parsing (plenty of libraries available for JSON parsing) and overall complexity it would also mean I cannot take advantage of content providers nor of SQLite's functionality.
So the question is, which model should I follow? Are there situations in which one is better than the other? Or are there better ways of handling this process that I am not aware of?
Aucun commentaire:
Enregistrer un commentaire