如何使用realm.io以递归方式删除具有一对多关系的记录?
例如,我有以下嵌套对象:
public class RealmUnit extends RealmObject {
private int id;
private RealmList<RealmChapter> chapters;
public RealmUnit() {
chapters = new RealmList<>();
}
public class RealmChapter extends RealmObject {
private int id;
private RealmList<RealmChapterGoal> chapterGoals;
public RealmChapter() {
chapterGoals = new RealmList<>();
}
}
public class RealmChapterGoal extends RealmObject {
private String type;
private String text;
public RealmChapterGoal() {}
}
答案 0 :(得分:3)
目前,Realm没有级联删除,但它位于我们的TODO上,并在此处进行跟踪:https://github.com/realm/realm-java/issues/1104
所以现在你唯一的选择就是手动自下而上的方法。
for(RealmChapter chapter: realmUnit.getChapters) {
chapter.getChapterGoals().clear();
}
realmUnit.getChapters().deleteAllFromRealm();
realmUnit.deleteFromRealm();
此处还介绍了一些解决方法: https://github.com/realm/realm-java/issues/2717