如何删除带有子关系的领域对象? [英] How to delete realm objects with their child relations?

查看:48
本文介绍了如何删除带有子关系的领域对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与其他对象有很多关系的大对象,这些对象也与其他对象也有关系.因此,当我删除根对象时,我发现只有父对象被删除而其所有关系都没有删除,是否有办法在同一事务中删除整个树?

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 ?

推荐答案

Realm目前不支持cascading delete.您可以在那里投票.在当前情况下,您似乎需要手动进行操作.

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

这篇关于如何删除带有子关系的领域对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆