Android Store a Parcelable Object in SQLite
Asked Answered
S

2

12

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?

Solvable answered 17/1, 2012 at 14:55 Comment(1)
you really need keep data in sqlite wouldn't be better to keep in a folder?Kwang
T
35

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.

Testament answered 17/1, 2012 at 15:20 Comment(2)
Like always, the perfect answer :) Anyway I don't know how much Serializable is scalable for backward compabilities. I think that I will try to store datas in JSON with some kind of compression :)Solvable
@StErMi: Yeah, if I were going down this path I personally would use JSON, or perhaps XML, unless some of the data can't possibly be turned into text. It's tons easier debugging when you can actually read the data, rather than it being a cryptic blob.Testament
U
3

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

Unlovely answered 17/1, 2012 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.