Q&A EmbeddedObject #2594
Replies: 3 comments 7 replies
-
|
I was reading up on this just last week and have some questions, hopefully this is an okay place to ask. From the documentation, the main draw seems to be added 1:1 relationship constraints, which I guess was not possible to guarantee until now. For non-mongodb users, what are the benefits of using
It would be beneficial if there was some information on if/how the internal structure differs here, with any advantages or drawbacks. |
Beta Was this translation helpful? Give feedback.
-
|
Hello ! I just discovered embedded objects, and try to migrate a 1:1 child table to it. The issue is, this child table has leftovers items that were not cascade deleted (app bug), thus has no parent. How can I delete those items in the migration code ? //all fails
migration.NewRealm.RemoveAll(nameof(DbReward)); //Does not compile
migration.NewRealm.DynamicApi.RemoveAll(nameof(DbReward)); //Can't remove Embedded Objects "The class DbReward represents an embedded object and thus cannot be queried directly"
migration.OldRealm.DynamicApi.RemoveAll("DbReward"); //OldRealm is read onlySo how am I suppose to delete those entries in the migration ? |
Beta Was this translation helpful? Give feedback.
-
|
@nirinchev Hello, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Q&A EmbeddedObject
This Q&A section aims to help you better understand the
RealmObjectsubtypeEmbeddedObject.We will add and answer common questions here. Feel free to add questions to this thread that might be helpful or create a new discussion if you want to talk about it in detail. In case you encounter bugs or problems you can always create an issue instead and we'll do our best to help you there.
What is an
EmbeddedObject?For a general overview, please refer to our documentation and the API reference.
What kind of relationship is modeled using an
EmbeddedObject?Adding a single
EmbeddedObjectlink to anotherRealmObjectleads to a 1:1 relationship.You can also use a
List<EmbeddedObject>which would be a 1:n relationship.This relationship is then enforced by the SDK. Such objects do have to have a parent and they cannot have more than one parent. Otherwise you'll get exceptions like the one's we've explained in more detail further down to help you understand if there is something that still needs to be solved. This has to be taken care of during the migration.
Can I change a
RealmObjecttoEmbeddedObjectand what about the migration?You can swap those two types if you need to.
If you change a
RealmObjecttoEmbeddedObjectyou need to write a migration for this step. During this migration you need to make sure that the 1:1 relationship of theEmbeddedObjectand all other classes using it is fulfilled. From then on everyEmbeddedObjectcan only be used by one and exactly one other (parent) object.EmbeddedObjects are not supposed to be queried but rather accessed via the object they are embedded in hence they don't have a primary key.What does the exception
At least one object does not have a backlink (data would get lost).mean?If you see above exception, please make sure that every
EmbeddedObjectdoes have a parent.If you don't need this
EmbeddedObjectanymore, you should delete it to fix this exception.An enhancement for this is planned which will offer an option to do the deletion automatically. At the moment this has to be done manually by writing a migration.
What does the exception
At least one object does have multiple backlinks.mean?This exception indicates that after the migration has been finished there were still
EmbeddedObjects that are used by more than one parent. This is basically the opposite toAt least one object does not have a backlink (data would get lost)..To solve this, you need to duplicate the
EmbeddedObjectand make sure every parent object has it's own copy to fullfil the 1:1 relationship.An enhancement for this is planned which will offer an option to automatically create such duplicates. At the moment this has to be done manually by writing a migration.
What does the exception
Cannot change table to embedded when using a primary key.mean?When changing
RealmObjecttoEmbeddedObjectyou need to make sure that the class does not have a primary key otherwise it cannot be changed to anEmbeddedObject.Are there performance benefits from splitting a large object into a parent object with multiple properties that are
EmbeddedObjects? Regardless of object size, are there performance benefits from using anEmbeddedObjectover a normal relationship (RealmObject)? Are cascading deletes cheaper than normal deletes?To answer this I first want to clarify that there are three options you could look at:
Street,House number,CityinPerson)RealmObjectto extract those properties into a child class (let's say theAddressinPerson)EmbeddedObjectto extract those properties into a child class (alsoAddressandPerson)In most cases, (1) will be faster than the other two options because everything is in the same place, no relationships needed. So in terms of usage (read/write) this would be a no, there aren't any performance improvements by extracting properties into another class (embedded or not).
The difference between (1) and (2) / (3) is more a structural one (and coding style to some extend). Especially if this new class (
Addressin our example) is shared with other classes (Person,Company, etc.) you would probably want oneAddressclass instead of repeatingStreet,City, etc. in every class that needs an address.The same is true for deletes. Deleting a single object (1) is going to be quicker than deleting relationships with multiple objects. There is, however, a difference between (2) and (3).
Cascading deletes of embedded objects will usually be faster than deleting nested
RealmObjects because this is implemented natively in our Core library whereas cascading deletes forRealmObjectwould have to be implemented by the user at the app level, thus requiring multiple managed->native transitions.Beta Was this translation helpful? Give feedback.
All reactions