Realm - Can't create object with existing primary key value

后端 未结 2 2115
暖寄归人
暖寄归人 2021-01-01 16:39

I have a object Person with many dogs. App has separate page where it shows just dogs and other page where it shows person\'s dogs

My model is as follows

<
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 17:26

    We don't need TiM's method anymore.

    Use add(_:update:).

    try realm.write {
        realm.add(objects, update: Realm.UpdatePolicy.modified)
        // OR
        realm.add(object, update: .modified)
    }
    

    Realm.UpdatePolicy enums:

    error (default)
    modified //Overwrite only properties in the existing object which are different from the new values.
    all //Overwrite all properties in the existing object with the new values, even if they have not changed
    

    NB: Works on Realm Swift 3.16.1

提交回复
热议问题