Read complete story

Saving Complex Objects using Android Room Library

In this blog, we will learn about saving complex objects using android room library.

If you have no knowledge about the android room, then you should first read this article:

Android Room Persistence Library

After reading this article, you get to know how easy now it is to create a table in your android application. It is just like creating a model class ( a Custom object).

This room library has saved us a lot from the boilerplate code that we used to write for creating a table.But when you start using in the real world scenarios, then you see that you have complex models which have one or more list of objects and some child objects. And room only recognizes the primitive datatypes.

Now you have two options :

  1. To restructure your model class and save the inner objects into separate tables and then manage them through relations.
    OR
  2. To use @Embedded annotation.

Before we go further, let’s first understand what this @Embedded annotation is and what it will do?

As per official docs: @Embedded can be used as an annotation on a field of an Entity or Pojo to signal that nested fields (i.e. fields of the annotated field’s class) can be referenced directly in the SQL queries.

What this means is that if you have some child object in your Model( Entity) class, then when creating the table, the number of columns will be automatically increased to fit in your child object as well. The total number of columns now will be ( Number of columns in your model class + number of columns(fields that make up your object) in your child object class).

But this will only help you save the child object and not the list as we have still not used anything that will help the room to identify the list of objects. for this purpose, we will use type converters.

Now, You will be wondering what exactly is a Type Converter?

Type Converter specifies additional type converters that Room can use. The TypeConverter is added to the scope of the element so if you put it on a class/interface, all methods/fields in that class will be able to use the converters.

Now enough of theory and explanation, let’s have a look at the coding part.

CODE :

Model(Object) Class: This class I need to save to my table.

User.class :

DataTypeConverter.class: This class will help Android room to convert the list into a primitive data type ( String) and save it in the table.

Ok, till now we understood the use of type converter, but still wondering what the @Embedded annotation did.

When Room will create your table.(my_table in our case). The table will have 5 columns ( 3 for the object that you are using and 2 for the embedded child object).

That’s it. Hope this helps you understand the room architecture and how you will be able to save the real world objects using room easily.

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close