如何正确删除 Realm child object?

How to delete a Realm child object correctly?

无法在文档或 Google 中找到关于此的具体信息,所以这里是示例:

class Parent: Object {
  let children = List<Child>()
}

class Child: Object {
  weak var parent: Parent?
}

当我想删除特定的 Child "child1" 时,我应该只使用:

Realm().write { realm.delete(child1) }

还是在parent里手动删掉就好(麻烦):

if let parent = child1.parent {
  if let idx = parent.children.indexOf(child1) {
    parent.children.removeAtIndex(idx)
  }
}
Realm().write { realm.delete(child1) }

谢谢!

我只是亲自测试过才确定;只需调用:

Realm().write { realm.delete(child1) } 

会自动将其从列表中删除。您无需亲自进入并从列表中手动删除对象。 :)