I need to write an application able to work offline, but also in the presence of a network minimize the number of repeated network requests. How to do I think. I will use the retrofit, loaders, sqlite. Cache invalidation will be on time (DateTime Column in table) Server API -> JSON -> Object -> Sqlite
1) When working in a network sqlite database will be filled with a mark of time when the data was obtained
2) When prompted again if the data on the time is not out of date (for example, less than 8 hours) available data in DB to be used.
3) If the data expires will be carried out repeated requests and update data in sqlite
4) If the network does not have and are not current, the application will use the old data
If a more technically correct option?
If possible, please provide the details of implementation, the names of classes, libraries, link to github etc... Thank you
P.S sorry for English)
Option 1: You can use a sync adapter to keep your data up to date. http://developer.android.com/training/sync-adapters/creating-sync-adapter.html
Option 2: You can define your cache invalidation rule. you can save in the preferences when was the last write operation was, if it exceeds 8 hours or whatever amount you decide, delete the whole database, table or just a row. And just save every request response to DB on receiving it.
PS you can also use okhttp with retrofit to cache your response for you in an LRU cache.
okHttpClient = new OkHttpClient.Builder()
.cache(new Cache(new File(Constants.CACHE_DIR, "http"), 10485760))
.build();
restApi = new Retrofit.Builder()
.baseUrl(RestApi.API_BASE_URL)
.client(okHttpClient).build();