Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm best practice to achieve cascade-like deletion

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 ?

like image 968
EmilDo Avatar asked Jul 01 '16 08:07

EmilDo


1 Answers

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.

like image 168
marius Avatar answered Nov 17 '22 11:11

marius