How To Store Objects In Android Room?
Solution 1:
I think the best way to answer this is a breif overview in storing structures...
Lists
Room does not support storing lists that are nested inside of a POJO. The recommended way to store lists is to use the foreign key approach. Store the List of objects in a seperate table (in this case a smallObjects table) with a foreign key to their related parent object (in this case "big_object_id"). It should look something like this...
@EntitypublicclassBigObject {
@PrimaryKeyprivateint id;
private User user;
@Ignoreprivate List<SmallObject> smallObjects;
}
@Entity(foreignKeys = {
@ForeignKey(
entity = BigObject.class,
parentColumns = "id",
childColumns = "big_object_fk"
)})publicclassSmallObject {
@PrimaryKey (autoGenerate = true)
privateint id;
private String smallValue;
@ColumnInfo(name = "big_object_fk")privateint bigObjectIdFk
}
Note that we have added the @Ignore
annotaiton to List<SmallObject>
as we want to ignore the field during Room persistance (as lists are not supported). It now exists so that when we request our list of related small objects from the DB we can still store them in the POJO.
To my knowledge this will mean you are making two queries.
BigObjectb= db.BigObjectDao.findById(bOId);
List<SmallObject> s = db.smallObjectDao.findAllSOforBO(bOId);
b.setsmallObjects(s);
It appears that there is a short hand for this in the form of @Relation
Type Converters
These are for cases where you have a complex data structure that can be flattend without losing information, and stored in a single column. A good example of this is the Date object. A Date object is complex and holds a lot of values, so storing it in the database is tricky. We use a type converter to extract the milli representation of a date object and store that. We then convert the millis to a date object on the way out thus keeping our data intact.
Embedded
This is used when you want to take the fields of all nested POJOs in your parent POJO and flatten them out to store in one table. an example :
- name
- age
- location
- x
- y
- DOB
..when embedded this structure would be stored in the database as :
- name - age - location_x- location_y- DOB
In a sense Embedded exists to save you time creating type converters for every nested object that contains primary type fields like String, int, float, etc...
Solution 2:
Convert Object/List<Object>
to String and then, Store it.
You can store the objects in Room Library as String. For that, you can serialize the object and store it as String in the Room Database.
Store to Room
Object -> Serialize -> String -> Store
Read from Room
String -> Deserialize ->Object -> Read
How to Serialize/Deserialize?
There are many options available. You can either do it manually or you can use a library for this. You can use Google's GSON library. It is pretty easy to use.
Code: Object -> String
publicStringstringFromObject(List<YourClass> list){
Gson gson = newGson();
String jsonString = gson.toJson(list);
return jsonString;
}
Code: String-> Object
publicList<YourClass> getObjectFromString(String jsonString){
Type listType = newTypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> list = newGson().fromJson(jsonString, listType);
return list;
}
Post a Comment for "How To Store Objects In Android Room?"