我将 Realm 用于我的 iOS 应用程序和一个表,我有一个主键“名称”
let application = Application()
application.domain = app.domain
application.name = app.name
realm.add(application)
现在当它尝试更新名称时会抛出错误
app?.name = newName
错误是
Primary key can't be changed after an object is inserted.
我该如何解决这个问题?
这是我声明主键的方式
override static func primaryKey() -> String? {
return "name"
}
最佳答案
一旦你插入一个带有主键
的对象,你就不能修改它:
来自 Realm Docs
primary key is added to a Realm, the primary key cannot be changed.
这让您几乎没有选择:
- 移除并重新插入对象
- 将主键更改为不变的内容,例如
id
- 省略
主键
。如果你不定义一个,你就没有主键,这意味着你可以有多个具有相同属性值的对象,并且不会被这个属性索引。
关于ios - 快速更新 Realm 主键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522285/