Reputation: 1503
I'm trying to figure out the best way to persist data for my Android application. The situation at the moment is there are objects created in one activity (UserItem) and objects created in another activity (SuggestionItem). I'm still thinking about structure at the moment but those will probably be subclasses of the same parent class (Item) with some common attributes like name
and location
.
SuggestionItem currently has a composition relationship with an object that holds most of its data. That object has type Suggestion, which has a few subclasses like RestaurantSuggestion and HotelSuggestion.
What I want to do is to have a third activity that displays a view of all the SuggestionItems and UserItems that are created. I was thinking of storing them in an SQLite database, as they should be saved between application closes/restarts. In the future there may also be some sort of exporting/importing, but I am ok with only thinking about saving these objects for now and redesigning the database later.
My question is, should I create a schema with tables for my classes and columns that match their fields, or should I instead serialize these objects and store the serialised version in the database? They're fairly simple; perhaps around 10 primitive and String fields per class (things like url
, pricePerNight
, imageUrl
). The only non-primitive field there is is a LatLng field, which could just be turned into a couple of double fields for latitude and longitide. I know about two serialization methods with Android: gson and implementing Serializable. I'm not sure which would be more appropriate.
Can anyone with more experience give me advice? Sorry for the wall of text.
Upvotes: 0
Views: 742
Reputation: 323
If you will never need to retrieve the items following a specific criteria serialization is good, but I suggest encoding individual fields into the database so that you can easily search for them.
As a general rule of thumb, encoding individual fields in the database is always better because you can't know now that in the future you'll need to retrieve your data following some criteria.
Note: this is taken from my comments to the question
Upvotes: 1