IOS领域:插入对象后无法更改主键

时间:2018-07-24 14:36:18

标签: ios objective-c realm

我在使用Realm方面有些新知识。我有一些代码:

RLMRealm *realm = [RLMRealm defaultRealm];
            [realm transactionWithBlock:^{
                self.object.objectPrimaryKey = someValue;
            }];

此字符串self.object.objectPrimaryKey = someValue;发生异常,原因为:'Primary key can't be changed after an object is inserted.'

objectPrimaryKey is a primary key. What can I do to fix it?

1 个答案:

答案 0 :(得分:1)

docs关于主键,您只能设置一次主键。

设置密钥后便无法更改: Override Object.primaryKey() to set the model’s primary key. Declaring a primary key allows objects to be looked up and updated efficiently and enforces uniqueness for each value. Once an object with a primary key is added to a Realm, the primary key cannot be changed.