我正在使用Realm.io作为持久存储的iOS应用程序. 我刚刚通过添加主键更新了我的一个自定义RLMObject子类. 当我运行应用程序时,我收到一条错误消息,告诉我需要添加迁移步骤: 'Migration i
我刚刚通过添加主键更新了我的一个自定义RLMObject子类.
当我运行应用程序时,我收到一条错误消息,告诉我需要添加迁移步骤:
'Migration is required for object type 'MY_REALM_OBJECT' due to the following errors: - Property 'property_name' has been made a primary key.'
我有其他迁移代码,但在Realm文档中找不到有关如何将主键添加到RLMObject的任何内容.
有人知道怎么做吗?
您需要使用键“primaryKeyProperty”并将值设置为newObject的迁移块中的RLMObject属性名称.primaryKeyProperty是需要迁移的RLMObjectSchema属性的名称.
[RLMRealm setSchemaVersion:kLatestSchemaVersion forRealmAtPath:theRealmPath withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) { if ( oldSchemaVersion < kLatestSchemaVersion ) { [migration enumerateObjects:MyRealmClass.className block:^(RLMObject *oldObject, RLMObject *newObject) { newObject[@"primaryKeyProperty"] = @"propertyName"; }]; } }];