Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is more efficient? Static, data Passing, shared preferences, Database...?

Tags:

android

I was recently doing an application in android and at some part I got a doubt that which is the efficient way of sharing Data. Like I can declare a static variable in one class and call that variable from the other class using Classname.Variablename or I could pass that data with my intent and get data from intent from the other class or I can use shared preferences and get data from it or I could even store that data in database and retrieve it from the other class from the database. What my doubt was which will be the most efficient way to do that (get data)? Since android applications are ultimately designed for phones memory usage and efficiency should be constraints. Could anyone guide me in the right path, it would be very helpful.

like image 992
SKT Avatar asked Mar 02 '12 07:03

SKT


People also ask

What are the advantages of using shared preferences over save instance state?

Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device.

What are SharedPreferences what are its advantages?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Is SQLite better than SharedPreferences?

With SQLite you use SQL language to create tables and then do insert, delete, update operations just like in any other database. But in case of shared preference you use just normal APIs provided by Android to write, read and update these values to a XML file.

How much data can SharedPreferences hold?

There's no hard limit. The main reason it is not recommended to use SharedPreferences in place of the database is mainly the performance -> shared preferences data is keept in ordinary flat XML file which lacks all the features SQLite offers.


2 Answers

My opinions:

Each one is best depends on the scenario. Let me explain.

Static Variable:

Static variable are common ways to give access to whole application context. If you want to maintain few datas which is not necessary to be maintained after the application exited, this is the best one.

Passing data through Intent:

This is not a kind of data storing. This is a kind of data sharing. I think this is the best way to pass data from activity to activity. But maintaining the keys for values in Constants is a good programming thing.

Shared Preferences:

Shared Preferences is nothing but a simple table which has two columns. (key, value).

advantages:

  1. Fast retrieval
  2. Easy to understand and program

cons:

  1. Maintaining Keys are difficult if we store a lot of values.
  2. User can clear this at any time.

Database:

When we have a lot of values to store with complex structure, we are left with only one great solution, ie. DB.

advantages

  1. We can maintain structure of data.
  2. Android has nice and simple APIs to handle sqlite operations.

cons

  1. Operation is little bit slow comparing to shared preferences.
  2. user can clear this at any time.

So we found the major problem is we can't maintain the persistence whatever storage we use. A simple and effective solution is to maintain data online and sync it with mobile db each time the application is started.

like image 142
Sadeshkumar Periyasamy Avatar answered Oct 11 '22 17:10

Sadeshkumar Periyasamy


I may be wrong, but i feel like i have to share my knowledge/experience... well the fastest and easiest way is to use static... but i will not recommend this.

My Selection would be: Database

Reasons:

1- By declaring static variable, it is not reliable, as at some stage, when your applications or some other application is taking too much memory, GC will try to collect all unreference objects, static is a good candidate here... like Activity A has static variables and you are currently in Activity B, now gc will remove A and its objects, there is a chance it will also collect static. Now selecting database is obvious here, i dont need to tell you because, within ur currect activity you can always retrieve data from database.

2- By declaring static variable, if its just a int or small datatype, thats not a big deal, but what if you are going to save a big list of complex structures, then there is a very high chance that your activity will leak memory which is also going to give u troubles. No leak if you properly use database.

SharedPreference/intent is also good/fast/memory effecient(i guess), but can get a little messy when passing large complex structure.

So to sum up, If you just want to pass a little structure (string + int) and your application isn't touching the limits of heap then u can use static but if you are doing some memory intense calls and passing somewhat large data then database is reliable, can be little slower than static but fast enough that we wont notice.

Hope it helps.

like image 30
Farhan Avatar answered Oct 11 '22 17:10

Farhan