-2

I am creating a data repository layer in my application which in turn serves data from two sources, either network API calls or getting data from local storage. When there is a request to the data repository layer, I first need to check if the data is present in the local storage or not. If not, I then make a call to my API to get the data. To check if the data is in local storage or not, I was thinking of two ways to implement this:

  • On app startup, I load all the data in the database into the memory (eg. lists, maps etc.) and then whenever I have to check of existence of data in the local storage, I check from the memory. This has a possible problem that I have faced earlier as well. When the app is in background, Android system, might clear up this allocated memory to serve memory to other apps. This makes things complicated for me.
  • Whenever I want to check the existence of data in the local storage, I directly make queries to SQL tables and decide based upon that. This is much more leaner and cleaner solution than the above mentioned case. The only worry for me here is the performance hit. I wanted to know if the SQLite database runs after being loaded into the memory or not? If yes, then the memory layer of data that I had created above is useless. If not, is it safe to keep on querying the SQLite database for each and every small data request?
Swapnil
  • 1,870
  • 2
  • 23
  • 48
  • You may find this of interest [In-Memory Databases](https://www.sqlite.org/inmemorydb.html) a mix of both and perhaps this [Advantages of an in-memory Database in SQLite](https://stackoverflow.com/questions/32833145/advantages-of-an-in-memory-database-in-sqlite) – MikeT Sep 12 '17 at 20:41

1 Answers1

1

SQLite caches some data, and the Android OS will keep the database file in its file cache, as long as there is enough memory.

So when using SQLite, your app's performance is automatically optimized, depending on available memory.

CL.
  • 173,858
  • 17
  • 217
  • 259