File or database? - Best practice for saving objects on an Android device

I am creating an android application in java where I define some objects like โ€œuserโ€ or โ€œplaylistโ€ etc.

How to save these self-defined objects on the device for later access?

Gson gson = new Gson(); String json = gson.toJson(user); 

I can parse objects through GSON in JSONObject or JSONArray. Now I have two options for storing strings: in the database or in a file. I know how to use android database classes and file / reader classes, but what is best for performance, accessibility, and especially simplicity?

+8
json android database file gson
source share
4 answers

It really depends on your use .

If your application data does not change much (think of a conference application that is used once or twice a year), you can use ContentProvider + CursorAdapters, which will simplify your life.

If your application data changes a lot (think of Gmail, where as many times as hundreds of emails can appear every day, or a news feed, or a social media application like Google+), then the database is likely to make your life worse. In this case, you must use a third-party caching system such as Google Volley or RoboSpice, etc., which handles all caching of JSON objects for you and all problems related to concurrency, etc.

+4
source share

The database scales well, the database mechanism solves the memory management problem, the database has good performance (if used correctly), the database mechanism allows you to run complex queries without writing a lot of Java code, the database mechanism allows you to insert or delete rows without writing everything file, the database is supported in Android widgets. Remember that the amount of memory that your application can use is limited, so if your JSON files become very large, you will not be able to fully load them into memory.

+4
source share

SQLite db, built into Android, will be a good solution for caching large amounts of information. For example, if you constantly retrieve data from the cloud and then plan to store it, you should use db. Dbs are designed to handle large amounts of data because they include indexing capabilities.

An example of caching JSON data with SQLite db can be found here: Caching downloaded JSON data to SQLite database is a good idea?

+1
source share

Use database . Your JSON can be easily parsed and read in the database. In addition, you can use SQL queries to find and manage data anyway in the future. A flat file will not give you the same flexibility. Files may be useful for other applications, but for JSON broken down into objects representing things - a database makes more sense.

+1
source share

All Articles