简体   繁体   中英

Update model Realm Swift

I'm a new in Realm and ask you to help me. I created the model:

class UserModel: Object {

    dynamic var email = ""
    dynamic var facebook_id = ""
    dynamic var google_id = ""
    dynamic var id = 0
    dynamic var name = ""
    dynamic var photo = ""
    dynamic var someinfo = ""
    dynamic var twitter_id = "" 

}

When I login to app, I can see my info on UserProfileController. Also I have a EditProfileController where I can change some info about myself. So when I successfully change it, I want to update my Realm model, and try to do this:

    let realm = try! Realm()

    try! realm.write {
        realm.create(UserModel.self, value: ["name": self.editEmail.text!, "email": self.editEmail.text!], update: true)
    }

But unfortunately I see this message:

''UserModel' does not have a primary key and can not be updated'

What if I want to update a few property at the same time? Can I do this with primaryKey? Or... How it possible to update Realm model without primaryKey?

问题出在这个update: true ,它用于自动替换领域中具有相同主键的对象,如果您的对象没有主键,则将update设置为false然后它将起作用并始终创建新对象

You need to set a primary key for your UserModel .

https://realm.io/docs/swift/latest/#primary-keys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM