How to delete a child Realm object?

Could not find specific information about this in a document or Google, so here is an example:

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

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

When I want to remove the specific child "child1", should I just use:

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

Or I need to manually delete it as a parent (cumbersome):

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

Thanks!

+4
source share
1 answer

I just tested it myself to be sure; just by calling:

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

will automatically remove it from the list. You do not need to enter and manually remove the object from the list yourself. :)

+7
source

Source: https://habr.com/ru/post/1606561/


All Articles