lundi 30 novembre 2015

Caching data in local database and API communication

I have Android application communicating with a server using REST API.
And caching is done using SQLite database and GreenORM.
Let me explain how it works.
Consider endpoint http://ift.tt/1NYOjzw

  1. I have ETage interceptor (OkHTTP interceptor).
    a) Firstly, before making request, it checks in local table if request(URL) is in etag table and has corresponding ETag hash from previous request-response. If it has If-None-Match header is added.
    b) Server checks if data for this request has the same hash it responses with 304 code, othrwise data and etag header goes into response. Simple ETag mechanism

  2. All retrieved data from request is stored in local db using insertOrReplace method ( I think the name is self-descriptive) primary key comes from the server.

  3. In case of no internet connection application can work in offline mode(just navigating through cached data)

The problem I have just noticed is that consider following example

Application requested all news but news entity has field verified if this field is not equal true news article should not be displayed.

By default http://ift.tt/1NYOjzw returns all news without filtering, but you can filter data using get parameters for example

http://ift.tt/1Iknhpz should return all verified news articles.

Sequence of steps

  1. Request all verified news http://ift.tt/1Iknhpz
  2. All data is saved into DB, for instance articles with ids from 1 to 10 were saved into db
  3. Article with id 5 was changed on the server to non-verified.
  4. Request again all verified articles http://ift.tt/1Iknhpz server will return -1 article (in our case ID 5).
  5. BUT here is the problem the record with ID 5 is stored in local DB and as far as I use inert or update it won't be deleted or updated. and will be displayed
  6. Another simple example is simple removal of item on the server.

My question is what is the best to solve this problem, and all possible problems that can appear in the future. It is obviously the problem in architecture of communication.
I have some ideas but they seems not as good as real solution should.

  1. Send each time IDs of articles stored in the local db and server should return the data and in case of removed somehow notify about this in response.
  2. Each time request is sent and received do following steps remove all data from local DB that satisfies the request before inserting new data (better solution)
  3. Simply remove data from the table before each request, but requests can be different ( but still in scope of one table) for instance different filters. So only the data from the last request will be saved in a table (bad solution, no benefits).

Please advice what is the best way in this case, maybe you can suggest something else according to the architecture.

Thanks.

Aucun commentaire:

Enregistrer un commentaire