How to update a list of objects in Realm DB Swift

本秂侑毒 提交于 2019-12-22 12:00:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!