Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a Realm child object correctly?

Tags:

swift

realm

Couldn't find specific info about this in the doc or Google, so here is the example:

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

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

When I want to delete a specific Child "child1", should I just use:

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

Or should I manually delete it in the parent like (cumbersome):

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

Thanks!

like image 691
hyouuu Avatar asked Feb 10 '23 02:02

hyouuu


1 Answers

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

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

will automatically remove it from the list. You don't need to go in and manually delete the object from the list yourself. :)

like image 160
TiM Avatar answered Feb 21 '23 01:02

TiM