I have 30 objects, each instance consisting of 7 string fields and a small bitmap.
These objects will be destroyed and recreated daily.
In my android app, would it be more efficient to write and read this data from a db? or to simply create a new class and create the 30 instances and store them in an array?
Memory efficient/performance/etc.
Thank you for your time.
If you do not want to write boilerplate code for database you can save your objects in JSON or XML format as files.
You got two ways:
Store in filesystem using serialization to store and deserialization to read
Store in JSON or XML natively and write converter between your class and needed data format
Database is good solution, because SQLite in Android is real fast and so on. But if you will want to change structure of table you will have to write ALTER TABLE queries in onUpdate() method of SQLiteOpenHelper and other troubles.
So I would recommend to use XML or JSON to store data.
Upd
Also, you can mark your custom data class as serializable, store values in ArrayList or other structure and serialize it to store, then deserialize to read.
Write the items to a database because they need to be persistent, if only for 24 hours.
You should store the image files in your Internal Storage sandbox and reference them by filename in the database because it is usually inefficient to store even the smallest Bitmap blobs in the database.
For a limited number of object I think you are batter with Object representation and storing them in an ArrayList. DB design for a large number of entries.
But if the data needs to be saved for all day you are risking in loosing it by a GC if you leave it in memory, in that case you have no option but to store it in the DB.
where are you getting this data from?
Related
For an application which will allow members of my organisation to see data on their mobile device i need to store pre-formatted data on the device so they can see it offline as well. How they get the data is trough a JSON request-response.
The response is formatted as follows (anonymised ofc):
[{
"firstname":"John",
"lastname":"Smith",
"group":"1",
"age":11,
"installed":"Ja",
"medical":"Is aan zijn linker zijde verlamd geweest.",
"notes":"Heimee. \r\nBeschermend opgevoed. \r\nTerug getrokken persoonlijkheid.",
"Insignes":["test", "Test2"],
"Parents":[]
},
{
"firstname":"Emely",
"lastname":"Watson",
"group":"33",
"age":14,
"installed":"Ja",
"medical":"",
"notes":"Test",
"Insignes":["Veilig & Gezond I","CWO II","CWO III","Kampeertechnieken & Pionieren"],
"Parents":[
{
"name":"ouder ouder",
"address":"op | 0000AA Amsterdam",
"phone1":"",
"phone2":"0612345678",
"mail":"example#google.com"
}]
}]
I have read a couple of discussion on how to best store this:
Is it ok to save a JSON array in SharedPreferences?
How to save JSON Array in SharedPreferences?
Android: what is the best way to store JSON data offline for the app in android?
Android - how to add JSON object to sharedPreferences?
What is the advantage of Using SQLite rather than File?
From reading these I have gathered that SharedPreference files are "faster" than sqlite but are prone to corruption. SQLite is a database and since the data comes from one I am inclined to use that at the cost of processing speed.
Now I only need to store and then read the data, it wont be mutated unless there is an update on the "main server" in which case I will probably wipe the local data and repopulate it. In these threads i have read that storing JSON in sharedpreference is easy but difficult to read.
But after reading these (and more) discussions I am no closer to knowing/deciding what the best way to store my json is.
Any advice is greatly appreciated!
You can try the ORMs like Realm or sugarORM and store the json has object in the database. They also provide the Cipher options by which you can encrypt the data too, Which would be more flexible.
http://www.androidauthority.com/use-android-keystore-store-passwords-sensitive-information-623779/
http://www.androidhive.info/2016/05/android-working-with-realm-database-replacing-sqlite-core-data/
If you don't need to use any ORM by third parties then you can directly encrypt the JSON string with the android keystore keys and then can store the encrypted string in the normal sqlite.
This is opinion based answer. Android no I wouldn't store JSON in a DB or preferences. Store the json in a file. A file can be accessed as stream the same way you stream access json. Virtually no upper limit on size in a file. I might store a link to the json file in a DB or preferences. Depending on application I might just extract json into abstract elements and store in DB for ordering selection.
The data you have provided depending on sensitivity I would extract and insert into the contacts database for the device.
The best option is to go with a database approach, for one it sounds your dataset might be large are rather expected to be large. Database persist large sets of information, however I do not recommend using SQLITE , for it is sql based, no ORM and the operations can get tedious and time consuming and mainly it is slow(debatable). Rather use the hottest and latest no-sql database for android and ios, which Realm, realm is super fast, it is based on java annotations and ORM(like hibernate). To work with json in android I recommend familiarizing yourself with GSON, A Java serialization/deserialization library that can convert Java Objects into JSON and back.
Shared Preferenences are really convenient to store small key value pairs, If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. You would of to still work with gson here, I never heard of a shared preference getting corrupted, what I do know is that they are fragile,and live with the existence of your app installation.
Conclusion: If your dataset is large use a database approach to persist large dataset, maintenance and modifications are more fluent here, however if you know that whatever you are storing is relatively small, use a shared pref, and to manipulate json constructs in android to and fro use the google gson library.
SharedPreferences :
If you have small amount of data in Json then you have to store it in SharedPreferences.
You can easily store Json to SharedPreferences using Gson. Just using this :
Convert Json to String :
Gson gson = new Gson();
String jsonString = gson.toJson(jsonData);
Convert String to Json :
gson.fromJson(jsonString, TypeToConvert);
Sqlite Database :
If you have large amount of data in Json then just go with Sqlite.
In my android app I load some data (JSON) from wed server. And I need to store that data.
All data, which I want to save - it is fixed number (for ex. 5) of JSONObjects with 30-50 fields.
Is this good idea to save each JSONObject in Preference? Or will be better to use SQLite?
note: why I ask?
save in Preference - implementation take 10 minutes.
use SQLite - implementation take one or two days, and required to
edit database, when I will add new fields.
I'd prefer database, and if you need 2 whole days for a reasonably simple database setup, you should perhaps take any chance to practice with it.
However, if you're sure you're never going to have any advantage of (complexer) queries, then saving it into a database might not give you any real advantages.
I would prefer sharedpreference here , because size of data is not too big. If size of data was too big and had many fields then database would be first choice. You can save the whole json String into preference and use it after parsing at run time.
My implementation choice would be a database table that stores the object as something you can then transform into a native intermediate object. By keeping the local data stored as JSON you're potentially locking yourself into keeping a JSON parser within the app for the foreseeable future even if your web service changes its format.
For example, if your web service changes to XML you're still going to have to keep the JSON parser in the app to handle those users who upgrade the app and have to do a data migration.
You could save JSON directly in SQLite database. If you parse data and then save in database then it would take some time for you to make everything work. But think about saving JSON directly in database. It will require two fields only in database link and json response.
Advantages:
- Big time saving
- You could save the same solution in your future apps for saving JSON in database.
Disadvantages
- You will not be able to query data
- You will have to parse every-time
I am developing a places of interest app which will display the list of places of interest in a location.
When user chooses one, it will display more information and address etc.
How do I store all this data? Currently I am using a text file to store all the data and subsequently when user chooses a place, it will parse the text file and retrieve the necessary data for display.
Any advice on what is a better way to do this? I looked at SharedPrefs, but it is more like storing "key-value" pair and in this case I need to store a large amount of data.
I want the info to be available even when the device is offline, thus I can't download from an online server upon request.
Any other way to do this?
You may store it to XML file using XML serializer, here is very good tutorial for learning that,
http://www.ibm.com/developerworks/library/x-android/
and it can be easily parsed using Java XPath Api. Have a look at this at parsing XML files
http://www.ibm.com/developerworks/library/x-javaxpathapi/
Use SQLite
It can store large data.
It is available offline.
All your problems will be sorted out.
Hre we have a wonderful tutorial for sq-lite
http://www.vogella.com/articles/AndroidSQLite/article.html
How about a relational database?
http://developer.android.com/training/basics/data-storage/databases.html
Take a look at Serialization. If you do not need database access, you could define a class what holds every information you need. Then, you can do the following:
when you need to save the datas, you serialize your object, dumping its content to a file, for example on the SD card
when you want to load the datas, you just load the above mentioned file, and get back everything from the dumped file
I am using this method in my app to cache some datas that would need internet access, so the user can still view it, and with proper implementation, this can work very nicely.
Use database, create table and insert all the data in it. When you need the data just fire the query, and you are done.
SQLite is fine for Android.
Depending on the type of data you want to store, you could use a SQLite Database (provided with Android) if it has a normal database structure. You could Serialize your data and save it in a raw or encrypted file, making you data implement Serializable.
I have an ArrayList of Location objects. The ArrayList typically has
from 1,000 to 10,000 Location objects.
I want to be able to persist the ArrayList and then read it back in
later.
I am assuming that it would be much faster to write out the entire
ArrayList of Location objects as a single file rather than storing
each Location object in SQLiteDB.
Can someone show me some example code to read/write an ArrayList of
Location objects, or suggest a better solution?
Are you really going to have to store all of the 10k locations, delete them all and store a new set of 10k locations every time? Or just make some updates occasionally? (such as delete or add several rows). If the latter, than you want to be using SQLite as it will be monumentally easier to do those actions with the Db.
Also note that SQLite is also just a stored file on the filesystem, there isn't a DB server in the way there is with MySQL, for example.
That aside, this has been discussed before on SO, so you may want to look at these:
Android data storage - File vs SQLite
Sqlite vs File based data storing?
I will develop an android application with a lot of data (json files with some rows and CSV for graphics data with a lot of rows) , this data change every 5 minutes and replaces all the previous data (or mostly).
What are the best approaches to design this ? I have 2 options:
Save all the data in a sqlite db, and sync this by a IntentService.
save the data in json and csv files and replace this every 5 minutes.
Which approach will the best performance?
This considering the time to parse the files, sorting data, the download time and the consistency of data.
any other ideas?
PD:I need a cache system too, in case if i don't have internet and I need the previous stored data
Advantages of SQLite:
Changes are ACID
You can make complex requests faster (e.g. "give me only fields A,B from items with (C/D)>E")
I would bet more compact for big data (integers are stored as integers instead of a string of individual digit)
Only one file
You can merge old data with new data easily
You can update current data, even while using it
Concurrency can be handled
Advantages for JSON/CSV:
Easier to debug (plain text)
Faster to make a whole update (copy the new file + delete the old one)
For the original question the whole delete/replace of the data makes JSON/CSV the winner.
However, if the application was to retrieve partial data every 10s and merge/update it with the previous one, SQLite would be a better option.
Sqlite is mostly used when you want data to be saved and used in future. In your case data is changing every 5 minutes so its better to have JSON because every time to make the Database connection store and retrieve after 5 minutes will take some time.
UPDATE:
I also had the same Application in which the data was changing every time. In that case I used Map<K, V> and ArrayList to maintain the values, because as the data is changing everytime I think its not feasible to store the data in Sqlite everytime. It needs much time to perform DB Connection store, retrieve, update the data in Sqlite.
I recommend using JSON or some type of object serialisation unless:
You need ACID compliance for write operations
You need to report against the data which may involve copying the data to an external RDBMS
or
You wish to join those complicit in the overuse / abuse of databases, as commonly seen nowadays
Ideally this should depend on whether you need the previous data, for maybe comparing it with current data and so on. As a thumb rule, I use SQLite when you need the data to be stored and retrieved at a later stage. In case the data is only for display, I would rather keep it in program memory. Mind you this does not involve file operation.
Purpose of JSON and SQLite is completely different from each other
JSON = is used send and receive data between server and client.
SQLite= is used to store data.