ios - 快速更新 Realm 主键值

标签 ios database swift realm

我将 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/

相关文章:

iOS MapKit 相机重置 6 到 354 之间的航向值

ios - 在 iOS 14 上请求跟踪授权总是返回 "denied"

android - 方法 startManagingCursor 和构造函数 SimpleCursorAdapter 已弃用

json - 如何在 Swift 上通过 Mashape 打印 JSON 数据?

swift - 在ios swift中登录后从wkwebview获取 token

android - 如何读取 Android 和 iOS 的 GRIB 文件?

c# - MonoTouch native 崩溃调用 CGPDFDictionary.GetArray

java - 多线程应用程序中的EntityManager?

mysql - SQL 高级 where - 合并查询中的两个 where 选择

ios - 当我更改 iPhone 的语言时,会导致崩溃吗?