Gday Folks,
Today I will talk about Embedded Objects. Previously we learned different types of Realm Relationships and how they are synced from mobile to Atlas.
What are Embedded Objects and Why should we use them?
Embedded Objects are a form of Relationships that map more naturally to the MongoDB Document Model. Specifically, embedded objects are objects which are nested directly within a parent object, which maps to the concept of embedded documents in MongoDB. Offline Realm relationships are very different from the MongoDB model, using embedded objects is recommended to make your schema model more adaptable with MongoDB Schema Design.
Generally, Embedded Objects are great for the same use cases as Embedded documents in MongoDB (or nested objects in JSON), when you have data where –
- There may be a strict parent/child relationship
- There may be value in having a hierarchical structure in the object
- Representing complex data within a single field is required
Embedded objects are not ideal if you want to create a relationship from many different documents to a single piece of information.
Embedded Object Data Model
The Author object could be added as Embedded Object in the Book class, There are more fields added to the Author class example for clarity
open class Book(
@PrimaryKey
var _id: ObjectId = ObjectId(),
@Required
var name: String = "",
var isRead: Boolean = false,
var _partition: String = "",
var authors: RealmList<Author> = RealmList()
): RealmObject() {}
@RealmClass(embedded=true) // this specification is important
open class Author(
@Required
var name: String = "",
var bio: String = "",
var email: String = "",
) {}
Embedded v/s Non-Embedded :
The main difference when working with non-embedded relationships is that Realm will need to pull a second object to traverse a relationship – similar to the concept of a $lookup
in MongoDB, or a JOIN in SQL. That being said, when working with data locally there is typically not a significant performance implication to this.
However, Embedded objects behave a bit differently compared to objects with a non-embedded relationship, these differences are derived from the embedded object being a part of another object as opposed to its own separate object –
-
Embedded objects can not have a primary key
-
An embedded object inherits the lifecycle of its parent object and cannot exist as an independent Realm object.
-
Realm will automatically delete embedded objects if the parent object is deleted
-
Very deep-level nesting of embedded objects should be avoided. This can exceed the MongoDB Document limit of 16 MB and deeply nested array objects can lead to sync issues in translating the writes to MongoDB.
-
An embedded object can have only one parent.
-
Embedded Objects can be the type of regular field, RealmList, or RealmDictionary.
Please Note: Only RealmList and RealmDictionary support having embedded objects as a value type (RealmSet’s and mixed types do not).
In the next session, I will talk about how to perform queries on embedded objects.
I would love to know your experience in using embedded objects in your mobile applications.
Cheers,