Android Store a Parcelable Object in SQLite - android

I need to store an object content in Sqlite.
I was wondering which was the best way to do it with a serialization of the object or with Parcelable.
Is it possible to store it as Parcelable? How can I do it?

You are welcome to convert your object into some sort of persistable data structure (XML, JSON, Serializable) and stuff it in some database column. Bear in mind that you will still need to deal with compatibility issues (e.g., Version 2 of your app changes a class, which now needs to deal with both Version 1 and Version 2 structures). Also bear in mind that, going this route, you lose a lot of database capabilities (e.g., querying on something in the object).
You are also welcome to experiment with object databases, or CouchDb, or storing your persistable data structure to a file, if SQLite is not a requirement.
What most certainly will not work reliably is to pour the Parcelable into a Parcel and try storing the Parcel. A Parcel is meant for IPC use only and is not designed to be persisted. This is one of the reasons why Parcelable is faster than Serializable.

If you need to persist data, use Serializable. Parcelable is meant for IPC use. It is a binary format and not recommended for persistence.

Related

Android Intent passing parcelable object vs passing Json string

I am aware that there are some answers to my question, but the answers are not very elaborate or convincing.
In my program, in order to transmit data from one activity to another, I'm converting the contents of the POJO class into a Json String and passing via bundle (Method1).
METHOD1
String jsonString = JacksonSingleton.getObjectMapper().writeValueAsString(object);
bundle.putExtra(KEY_STR, jsonString)
In Method 2, I am passing a Parcelable object in the bundle.
METHOD2
bundle.putExtra(KEY_STR, parcelableObject);
If I were to implement Method 2, I'll have to implement Parcelable interface and write custom code for marshaling and unmarshaling my POJO class fields.
My question, which of these methods is better/recommended in terms of performance? Method 1 is very convenient but I want to follow best practices.
EDIT:
If you hate writing 'Parcelable' boilerplate code like me, you could use the Parcelable code generator plugin with your Android Studio/IntelliJ IDE. It will auto generate the methods for marshalling and unmarshalling your class fields. Very easy to use and highly recommended.
As much as I hate answering my own questions, I thought of sharing my observations that could help other developers in future.
Transmission of data from one activity to another can be done using either passing Serializable or Parcelable Objects in an activity intent. Android developer website recommends using Parcelable interface for this purpose.
However, my question was pertaining to the efficiency comparison between passing Parcelable object and JSON string.
To test this, I used an old and low-end Android device. I launched an activity by sending a large Parcelable Object in an activity intent. Next, I launched the same activity using the JSON String of the same object in the activity intent. What I observed was a significant observable latency while launching an activity by sending a JSON String instead of Parcelable Object.
In conclusion, even if we pass a JSON String, Java String object always implements Serializable. Google recommends using Parcelable instead of Serializable objects. This will usually be insignificant in case of strings of negligible length. However, in the case of massive Json Strings of massive objects, The efficiency will certainly take a toll.
You could refer this for performance benchmark of Parcelable vs Serializable.
TLDR:
Parcelable - More boilerplate code, better performance and a better engineering practice overall.
Serializable - Less code, easy to learn and acceptable if you're not obsessed with performance/best practices.
The JSON object/array is global usage, and can transfer over internet, we can handle it in the same way by different programming language on different platforms, and is easily stored. If the machine is not so slow and the data is not so huge, I prefer use JSON.
What for put extra with a large data to send to another activity? Sometimes I just send an ID for stored data or use a singleton to handle object... (Can another app access the Parcelable object?)
So the Parcelable is for cross process used in the same app.. Just like RemoteView and Service use it to transfer data for convenient...

Is it a good practice to serialize Parcelable objects for persistent storage?

I have two objects that implement the interface Item:
public interface Item extends Parcelable{//unneeded stuff}
I want to store an ArrayList<Item> persistently. Is making the Items Serializable a good practice?
Neither Parcelable nor Serializable are a good idea for persisting data. Store persistent data in SharedPreferences if possible, or wind it out to a file.
Serializable could hit you if you decide to change the structure of the object. Parcelable is not guaranteed to remain constant across versions; it's designed to pass data between processes, not store data.
If you want to store data long-term, you should write it to a file. Depending on how you want to access it, you may want to store it in a database or a content provider.
Usually you make an object parcelable/serializable depending on your need. If you need that data to recreate your UI, pass into the bundle etc. then you would make it serializable.
Making object Serializable doesn't force you to use any particular serialization method. Generic Java serialization is quite inefficient, but nothing prevents you from making Serializable use Android parcelization (just marshal Parcel to the stream in writeObject)
But I doubt you really need serialization for data persistence. Shared preferences or database work better in most cases.

How to write to and read data from Android's ApplicationContext?

In my app I need a central storage object that will be accessed from different parts of the application (like a singleton data holder).
AFAIK the clean way to implement singletons in Android is to use the ApplicationContext.
How can I
put data (like instance of List<MyPieceOfInformation>) in the ApplicationContext and
get them out of it
?
Is it correct that the only way to store more or less complex data in Android is to use the built-in SQLite database?
you can use mysql and others as well.
it is all depends if you want to save the data in local or external.
as external, for example, you can use mysql and web server, then communicate using json.
for saving List, you can use static.
In my app I need a central storage object that will be accessed from different parts of the application (like a singleton data holder).
Then use a singleton.
AFAIK the clean way to implement singletons in Android is to use the ApplicationContext.
First, there is nothing in Android named ApplicationContext. You probably mean Application.
Second, in the opinion of many experts (myself included), a custom Application is less "clean" than regular singletons.
Is it correct that the only way to store more or less complex data in Android is to use the built-in SQLite database?
Comparing a singleton to a database is like comparing an apple and an asteroid, on the grounds that both are made of matter and, in English, begin with the letter "a".
A database is persistent. You use a database when you want to save data persistently.
A singleton is not persistent. You use a singleton for transient data, such as a cache of data that is backed by a database.

Android - How to store Address to DB

Looking to store android.location.Address to a SQLite database. I am using ORMLite to persist my objects. ORMLite can persist Serializable items (as a BLOB I believe) but I think the only way to get something Serializable from an Address is to write it into a Parcel. Then I took a look at Parcel here: http://developer.android.com/reference/android/os/Parcel.html and it says it should not be used for general purpose Serialization mechanism. So I am just wondering what the best practice for doing this would be. I do not want to store the Address in contacts, strictly in my SQLite database. I am currently doing this with my own Address class (very simple) but would prefer to use the built in Android class for this.
thanks
The answer from the user list was to define a companion object that will be stored to the database and do the translation to/from the android.location.Address object by hand -- unfortunately. This will allow you to manager the storing of the various Address fields to the database without worrying about forwards and backwards compatibility with other Android versions.
Here's the discussion on the ormlite-user mailing list.

Android, Serializable/Parcelable problem in client-server app

I want to send complex data from my android to a remote server via TCP-sockets. I know that I need to serialize the Objects. In Android this is done via parcelable. But this is an android specific interface and the server only knows the serializable interface. Also vice-versa android doesn't know a serializable interface.
Both the android and the server must "know" the object but they are implemented in two different ways (server--> serializable, android --> parcelable)
How do I use these interfaces properly, so that I can send Objects via TCP to the server successfully?
Why not use Simple XML serialization, works with both Android and Java 1.5+. Its located at the following site.
http://simple.sourceforge.net
Also, the framework is fairly small and suitable for mobile platform (approx 270K with no dependencies). And it much more performant than most XML serialization or binding frameworks.
We had the same issue here at work and we switched to JSON. Maybe you should consider doing the same?!
Edit: Android does know the Serializable interface. How could I forgot...
According to http://developer.android.com/reference/android/os/Parcel.html it is not appropriate to use a parcel for persistant (or network) serialization:
Parcel is not a general-purpose serialization mechanism. This class
(and the corresponding Parcelable API for placing arbitrary objects
into a Parcel) is designed as a high-performance IPC transport. As
such, it is not appropriate to place any Parcel data in to persistent
storage: changes in the underlying implementation of any of the data
in the Parcel can render older data unreadable.

Categories

Resources