jeudi 2 juillet 2015

Robospice using caching in a dynamic ormlite database

I have an android app that loads data from a server and displays it in an endless scroll in a recyclerview.

It loads 5 items per page and if you scroll down, it triggers another 5 page to load.

Each time I scroll down, robospice is triggered and the 5 items will be cached using the current page number as the cacheKey into a SQLite database through ORMlite. The array of items returned by my server will then be added onto the total list of all items which I saved in an arraylist. This total list is then passed to my recyclerview adapter, which updates the recyclerview.

Therefore:

(cacheKey/page number:1) - first 5 items

(cacheKey/page number:2) - next 5 items

(cacheKey/page number:3) - next 5 items

Now I can also delete items on my android app. So therefore, if I don't like item 4 on page 2 for example, I can swipe left and delete it.

Since I don't keep track of the pages in my arraylist (it is a total list of everything), I can't really go:

  1. spiceManager.getFromCache() - fetches the items from cache but since I don't keep track of the page in which the item was loaded from (remember, I load from the total arraylist), I do not have the cache key. I can't tell if the 9th item was loaded from the 2 page, for example, and therefore I don't have the cache key to fetch it from the cache.

  2. spiceManager.removeDataFromCache() - delete the data and the associated cacheKey for those 5 items but save the 5 items temporarily into a arraylist.

  3. spiceManager.putInCache() - I would delete the item in the temporary arraylist and then put the data from the arraylist back into cache with the same cacheKey for the 4 items as 1 is now removed

What I can do however is this:

  1. spiceManager.removeAllDataFromCache() - remove all the data from the cache / SQLite DB.
  2. spiceManager.putInCache() - put into cache all the data excluding the item that was previously deleted and issuing out a new cache key.

What I'm worried about with this method however, is that it is highly resource intensive. Each time you delete something, the whole DB is deleted and resaved without the deleted item.

I think that if I want to add a new item dynamically to my SQLite DB, this issue would also come about so I would also have to delete the whole DB and re-add it the whole arraylist into my SQLite and then re-issue a cacheKey.

Are there any other strategies out there for what I want to do when I want to make changes to my SQLite cache?

Thanks!

Aucun commentaire:

Enregistrer un commentaire