向RLMObject添加主键需要迁移,任何想法如何? [英] Adding a primary key to a RLMObject requires migration, any ideas how?

查看:538
本文介绍了向RLMObject添加主键需要迁移,任何想法如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Realm.io作为持久存储的iOS应用程序。
我刚刚更新了一个我的自定义RLMObject子类通过添加一个主键。

I'm working on an iOS app with Realm.io as the persistent store. I've just updated one of my custom RLMObject subclasses by adding a primary key.

当我运行应用程序,我得到一个错误告诉我,我需要添加迁移步骤:

When I run the app, I get an error telling me I need to add migration steps:

'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添加主键的任何内容。

I have other migration code but can't find anything in the Realm docs on how to add a primary key to an RLMObject.

任何人都知道如何做到这一点?

Anyone know how to do this?

推荐答案

使用键primaryKeyProperty,并将值设置为newObject的迁移块中的RLMObject属性名称。
primaryKeyProperty 是需要迁移的RLMObjectSchema属性的名称。

You need to use the key "primaryKeyProperty" and set the value to the RLMObject property name in the migration block for the newObject. primaryKeyProperty is the name of the RLMObjectSchema property that needs to be migrated.

[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";
          }];
     }
 }];

这篇关于向RLMObject添加主键需要迁移,任何想法如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆