Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete realm objects with their child relations?

Tags:

android

realm

I have a big object that has a lot of relationships with other objects and those objects have relationships with other objects as well. So when i delete the root object, i found out that only the parent object is being deleted while all its relationships are not, is there way to delete the whole tree in the same transaction ?

like image 504
Zeyad Gasser Avatar asked Nov 04 '16 15:11

Zeyad Gasser


People also ask

How do you delete a realm object?

To delete an object from a realmrealmRealm is an open source object database management system, initially for mobile operating systems (Android/iOS) but also available for platforms such as Xamarin, React Native, and others, including desktop applications (Windows), and is licensed under the Apache License.https://en.wikipedia.org › wiki › Realm_(database)Realm (database) - Wikipedia, pass the object to Realm. delete(_:) inside of a write transaction. // Previously, we've added a dog object to the realm.

How do I delete data from realm in react native?

To delete all objects from the realm, call Realm. deleteAll() inside of a write transaction.


1 Answers

Realm doesn't support cascading delete for now. You can vote for that feature there. In the current case, seems you need to do it manually, one by one.

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RootObj root = realm.where(RootObj.class)
                            .equalTo(RootObjFields.ID, rootId)
                            .findFirst();
        if(root != null) {
            if(root.getChild() != null) {
               root.getChild().deleteFromRealm();
            }
            root.deleteFromRealm();
        }
    }
});
like image 66
Divers Avatar answered Oct 29 '22 03:10

Divers