Best practice for keeping data in memory and database at same time on Android

后端 未结 2 1617
死守一世寂寞
死守一世寂寞 2020-12-12 15:54

We\'re designing an Android app that has a lot of data (\"customers\", \"products\", \"orders\"...), and we don\'t want to query SQLite every time we need some record. We wa

2条回答
  •  伪装坚强ぢ
    2020-12-12 16:14

    The problem with a memory cache is of course that you need to keep it in sync with the database. I've found that querying the database is actually quite fast, and you may be pre-optimizing here. I've done a lot of tests on queries with different data sets and they never take more than 10-20 ms.

    It all depends on how you're using the data, of course. ListViews are quite well optimized to handle large numbers of rows (I've tested into the 5000 range with no real issues).

    If you are going to stay with the memory cache, you may want have the database notify the cache when it's contents change and then you can update the cache. That way anyone can update the database without knowing about the caching. Also, if you build a ContentProvider over your database, you can use the ContentResolver to notify you of changes if you register using registerContentObserver.

提交回复
热议问题