'Caching JSON data on android: Room vs file

An app retrieves JSON formatted strings from a network and parses them for presentation to the user.

What would be the best way to cache the data in case it's needed again later on or in a future session?

I am considering two options: Room and local file storage.

If the data is stored in a file, it has to be parsed every time it is required; but I assume that retrieving data from a Room database takes some time. Does anyone know how these two operations compare from a computing cost point of view.

Also, assuming I would store all the data in the JSON strings, which option would use the least storage space?

Thanks, Marc



Solution 1:[1]

There are some things I would like to point it out:

File:

  1. If you're storing the data in a file, you'll probably need to access it multiple times to do your CRUD.
  2. You would have to parse the data everytime you need it.
  3. Your app would have to ask permission to the user to handle the files.

Room:

  1. Room is an SQLite abstraction developed to do exactly what you want, which is store the data locally.
  2. Room has support for Kotlin coroutines, in this case, DAO methods can be marked as suspending functions to ensure that they're not executed on the main thread. Therefore, your app won't freeze when doing CRUD operations.
  3. Compared to storing the data in a file, Room gives security to your data.
  4. Using local is faster than store data in a file.

In my opinion, you should use Room and implement an Single Source of Truth (SSOT) in your repository/use case.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Junior Mourao