Storing objects in realm store


#1

Can we store objects or nested objects in realm. I have a field which is an object. Is there any way to store that field. eg : obj1 : { type: ‘any string’ } -> Can I store something like this in realm ?


#2

Yes, of course.
Simply use 2 objects :slight_smile:
Like: Obj1 { data: Obj2 }
Obj2 { … }


#3

Does this mean we have to create a separate table and give it’s ref here ??

like my current object (table) has multiple fields…most of which are strings. One of the field is object.

Eg -
Account - table
field list -
name: ‘string’,
age: ‘string’,
occupation:‘string’,
address : {
line1 : ‘string’,
line2: ‘string’
}

so here will address be a different table altogether with 2 fields - line1 & line 2 ?


#4

There are no tables in realm, it is not a relational database.

If you use the same Address model then it will be stored in the same objects (aka. you can access both of them when you write a query on the Address models)


#5

realmObj.properties[Table_Entry_Id] = ‘string’;
realmObj.properties[Address] = {type: ‘object’ , objectType:‘string’}

this.realmDbInstance.create(‘XYZ’, realmObj);

Now this is giving me error:
Schema validation failed due to the following errors:

  • Property ‘XYZ.Address’ of type ‘object’ has unknown object type ‘string’

#6

Thats not how you can make parent objects.

You can find a good documentation about the objects here: