How to delete realm objects with their child relations?

一个人想着一个人 提交于 2020-07-05 08:09:08

问题


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 ?


回答1:


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();
        }
    }
});


来源:https://stackoverflow.com/questions/40426414/how-to-delete-realm-objects-with-their-child-relations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!