I am refactoring my app with the great Realm library, replacing CoreData. One issue that I am facing is the lack of cascade deleting, and missing any good suggestion of how to do this important step in another way.
My model can be described as
Person - idx, <RLMarray Dogs>
Dog - idx, <RLMarray Walks>
Walk - distance, time
What is the best practice when deleting Person and all its releted dogs and dog.walks ?
Iterating over all ? or is there any other method available to get the reference of the children objects ?
For now, you'd need to traverse all related objects and their related objects and so on yourself while avoiding cycles and only deleting objects over relations on which deletes should be cascaded.
This could be achieved depending on the object model by implementing a recursive method on each involved object e.g. cascadeDelete
, which calls the method on related objects. Deleting the object itself must be always the last step, because otherwise the object is invalidated and its relationships are not accessible anymore.
Beside that, it might be helpful to know that there is a method -\[RLMRealm deleteObjects:\]
which lets you delete NSFastEnumeration
-implementing collections what includes RLMArray
and RLMResults
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With