I'm trying to find the right way to update a list in a Realm object. For example:
class Person: Object {
@objc dynamic var id: String = ""
let children = List<Child>()
}
//Adding an item to the list
func add(child: Child, _ completion: (DBError?) -> Void) {
do {
let ctx = try Realm()
if let person = ctx.objects(Person.self).last {
try ctx.write({
person.children.append(child)
completion(nil)
})
} catch {
completion(DBError.unableToAccessDatabase)
}
}
This seems to work for adding an element. So how do I update an individual element in the array as well as replace the whole array to ensure it persists?
来源:https://stackoverflow.com/questions/49546847/how-to-update-a-list-of-objects-in-realm-db-swift