2015-06-09 176 views
3

我试图删除我的一个Realm对象中的属性,但是我不确定如何为此写入迁移。删除Realm对象中的属性

我只是删除从我的对象的头文件的属性,但因为我得到这个错误,没有工作:

Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'Stock' due to the following errors: - Property 'percentageOn' is missing from latest object model.'

我知道怎么写迁移添加字段,但我怎么去除呢?

+1

当然你不能直接做没有结果。解决方案1:删除U这样的属性,并从模拟器中删除应用程序。这解决了数据库不一致的问题。解决方案2:您的应用正在投入生产,用户已经在使用它。因此,您需要执行迁移以不影响您的客户。请阅读文档在这种情况下该怎么做:https://realm.io/docs/objc/latest/#migrations – David

回答

3

大卫说的是正确的。如果您确保正确执行迁移,则Realm可以轻松处理已删除和添加的属性。除非你居然还需要percentageOn的价值,你甚至可以离开迁移块空就像在领域网站的例子:

// Inside your [AppDelegate didFinishLaunchingWithOptions:] 

// Notice setSchemaVersion is set to 1, this is always set manually. It must be 
// higher than the previous version (oldSchemaVersion) or an RLMException is thrown 
[RLMRealm setSchemaVersion:1 
      forRealmAtPath:[RLMRealm defaultRealmPath] 
     withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) { 
    // We haven’t migrated anything yet, so oldSchemaVersion == 0 
    if (oldSchemaVersion < 1) { 
    // Nothing to do! 
    // Realm will automatically detect new properties and removed properties 
    // And will update the schema on disk automatically 
    } 
}]; 

// now that we have called `setSchemaVersion:withMigrationBlock:`, opening an outdated 
// Realm will automatically perform the migration and opening the Realm will succeed 
[RLMRealm defaultRealm]; 
+0

好的,在一个迁移块内,Realm检查任何已删除的属性并将它们从数据库中删除? – Sean

+0

正确!也就是说,这些属性中的数据也将被删除,所以如果您需要保留该数据,请确保将其移至迁移块中的新属性。 – TiM