2015-10-27 42 views
18

我有一个对象NotSureItem,其中有三个属性'标题',其名称已从'text'和textDescription中重新命名,后面我添加了一个dateTime属性。现在当我要运行我的应用程序时,它会崩溃,当我想添加一些东西到这些属性。它显示了以下声明。RLMException,对象类型需要迁移

'由于以下错误,对象类型'NotSureItem'需要迁移: - 最新对象模型缺少属性“文本”。 - 属性'标题'已被添加到最新的对象模型。 - Property'textDescription'已被添加到最新的对象模型中。'

这里的我的代码:

import Foundation 
import Realm 

class NotSureItem: RLMObject { 
    dynamic var title = "" // renamed from 'text' 
    dynamic var textDescription = "" // added afterwards 
    dynamic var dateTime = NSDate() 
} 

回答

74

只要删除您的应用程序并重新运行。

每当您更改Realm对象的属性时,您现有的数据库将与新的对象不兼容。

只要你还处于开发阶段,你可以简单地从模拟器/设备上删除应用程序,然后重新启动它。

当您的应用程序发布后,如果您更改了对象的属性,则必须实施迁移到新的数据库版本。

+0

为什么downvote? – joern

+1

它适合我@joern –

+2

这是正确的答案。只是有人低估了它。你能否接受答案,如果它为你工作,所以这被标记为一个正确的答案? – joern

5

下面的代码为我工作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; 
config.schemaVersion = 2; 
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) { 
    // The enumerateObjects:block: method iterates 
    // over every 'Person' object stored in the Realm file 
    [migration enumerateObjects:Person.className 
        block:^(RLMObject *oldObject, RLMObject *newObject) { 
    // Add the 'fullName' property only to Realms with a schema version of 0 
    if (oldSchemaVersion < 1) { 
     newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", 
          oldObject[@"firstName"], 
          oldObject[@"lastName"]]; 
    } 

    // Add the 'email' property to Realms with a schema version of 0 or 1 
    if (oldSchemaVersion < 2) { 
    newObject[@"email"] = @""; 
    } 
    }]; 
}; 
[RLMRealmConfiguration setDefaultConfiguration:config]; 

// now that we have updated the schema version and provided a migration block, 
// opening an outdated Realm will automatically perform the migration and 
// opening the Realm will succeed 
[RLMRealm defaultRealm]; 

return YES; 
} 

更多信息:https://realm.io/docs/objc/latest/#getting-started

3

你修改数据库不再与保存的数据库这就是为什么需要迁移兼容。您的选择是删除旧的数据库文件并重新启动(如果您处于初始开发阶段,则工作良好),或者如果您在现场,请执行迁移。

您可以通过定义架构版本并在您的Realm配置中提供数据库迁移“脚本”来实现此目的。整个过程记录在这里(连同代码样本):here

2

您可以在启动消除数据库是这样的:

[[NSFileManager defaultManager] removeItemAtURL:[RLMRealmConfiguration defaultConfiguration].fileURL error:nil]; 
10

删除应用程序,并重新安装是不是一个好的做法。我们应该在第一次遇到移民需求时,在开发过程中纳入一些迁移步骤。由SilentDirge提供的链接很好:realm migration document,它为处理不同情况提供了很好的示例。

最少迁移任务,从上面的链接下面的代码片段可以自动完成迁移,并与AppDelegate中的disFinishLaunchWithOptions方法一起使用:

let config = Realm.Configuration(
 
    // Set the new schema version. This must be greater than the previously used 
 
    // version (if you've never set a schema version before, the version is 0). 
 
    schemaVersion: 1, 
 

 
    // Set the block which will be called automatically when opening a Realm with 
 
    // a schema version lower than the one set above 
 
    migrationBlock: { migration, oldSchemaVersion in 
 
    // 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 
 
    } 
 
    }) 
 

 
// Tell Realm to use this new configuration object for the default Realm 
 
Realm.Configuration.defaultConfiguration = config 
 

 
// Now that we've told Realm how to handle the schema change, opening the file 
 
// will automatically perform the migration 
 
let _ = try! Realm()

相关问题