How to save an array of Android Parcelable to a file? - android

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?

Related

Storing a large amount of backend data in Android

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.

More efficient in Android app? Database or Objects

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?

ArrayList or SQLite

In terms of storing data in Android, would it be more efficient to use a large ArrayList or setup an SQLite database? I have ~9000 bus stops and stop names (both in String) that I need to store and I was wondering which method would be the quickest or most efficient.
An ArrayList is probably a very bad idea. I assume you want the data to be persistent, so if your user kills your app your data will be lost if you use an ArrayList. A database is persistent(unless the user clears the cache of the app). So I would highly recommend using a database over an ArrayList.
If your data does not change then you could probably have it read on startup and kept in memory while the App runs. For example, having a .json file with all the stops, read it on startup, put the data in a HashMap or something that is easy to use since you will probably lookup stuff. It would probably work fine with ~9000 entries.
If you are going to add or update information about the stops during usage then the SQLite is the better choice though.
1.) Store and retrieve your data from a SQLite DB since you want persistance. And since you say you have 9k+ rows a simple select will give you everything at once and you can easily filter the data as well if you need to
2.) At startup, put all your data into efficient memory structures like HashMaps(or in your case arraylists) and reference them throughout the app. This way you'll only do one time access.
3.) When in doubt build a DB. No harm, more efficient and easier to handle than files

What method should i use to store text and pictures on the phone?

I am making an expense log application. The user can create a log by adding a picture of an object and then add the price and some comments to it.
I would like to know if I need to use database to store that data or i can store it directly to the phone.
There are several ways you can store data in Android application: SharedPreferences, files, SQLite databases, etc. Which one you choose, depends on your requirements, amount of data you need to store, etc. I suggest you read Data Storage section in the Android developer's guide to get started.
For your case a databse is the best fit. You could put all expenses in a String array in the SharedPreferences but this would be horrible to use. You would always need to load all expenses to memory if you are searching for one and so on. A database allows searching, filtering and ordering through SQL. It is a bigger initial amount to create the Database but the management of the data will be much nicer afterwards.
Demonick is right about the images, only store the path to the image file in the database and then retrieve the images from there. If you are storing images on the SD-Card the user or other apps can access and delete them so don't count on them to be available later.

JSON file VS SQLite android

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.

Categories

Resources