Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File or Database? - Best Practice to save Objects on Android-Device

I'm building 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 the objects via GSON to a JSONObject or a JSONArray. Now I have two options to save the strings: In a Database or a File. I know how to use the android database-classes and the filewriter/reader classes, but what is the best practice with regards to performance, accessibility and especially to simplicity?

like image 488
JU5T1C3 Avatar asked Aug 02 '13 12:08

JU5T1C3


People also ask

What is the best way to store data in Android?

Shared Preferences is the easiest to use, especially if you want to store discrete primitive data types. However, internal and external storage is best for storing files such as music, videos, and documents, while SQLite wins if you need to perform fast searches and queries on your data.

What is the best database to use with Android?

PostgreSQL. A unique relational database, PostgreSQL is the best database for Android and iOS apps. Developers can customize this database as they want; that's why it's the most preferred mobile app database.

How can I store data on Android without database?

You can use application preferences to store data as shown here (if the data is small enough). SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences", MODE_PRIVATE); SharedPreferences. Editor prefEditor = gameSettings. edit(); prefEditor.


Video Answer


1 Answers

It really depends on your use case.

If your application data doesn't change a lot (think a conference app that is used once or twice a year), then you can use a ContentProvider + CursorAdapters which will make your life easier.

If your application data changes a lot (think Gmail, where as much as hundreds of email can pop-up every day, or a news feed, or a social networking app like Google+), then a database could probably make your life worse. You should use in this case a 3rd party caching system, like Google Volley or RoboSpice, etc. which handles all the caching of JSON objects for you and all the concurrency problems that appear, etc.

like image 185
Bogdan Zurac Avatar answered Oct 24 '22 00:10

Bogdan Zurac