RLMException,对象types需要迁移

我有一个对象NotSureItem,其中我有三个属性'标题'的名字是从'文本'和textDescription我已经添加了以后和一个dateTime属性重命名。 现在,当我要运行我的应用程序时,它会崩溃,当我想添加一些东西到这些属性。 它显示以下语句。

对象types“NotSureItem”需要迁移,原因如下: – 最新的对象模型缺less属性“文本”。 – 属性“标题”已被添加到最新的对象模型。 – 属性“textDescription”已被添加到最新的对象模型。

这里是我的代码:

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

只要删除你的应用程序并再次运行。

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

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

稍后,当您的应用程序已经发布并且您更改了对象的属性时,您必须实施到新数据库版本的迁移。

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

对于最小迁移任务,以上链接中的以下代码片段可以自动执行迁移,并与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() 

下面的代码正在为我工​​作

 - (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

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

您可以通过定义架构版本并在您的Realmconfiguration中提供数据库迁移“脚本”来实现这一点。 整个过程logging在这里(以及代码示例): 在这里

你可以像这样清除数据库:

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