RLMException,对象类型需要迁移
RLMException, Migration is required for object type
我有一个对象 NotSureItem
,其中我有三个属性 title
,其名称是从我后来添加的 text
和 textDescription
重命名的,还有一个 dateTime
属性。现在,当我要去 运行 我的应用程序时,当我想向这些属性添加一些东西时它崩溃了。它显示以下语句。
'Migration is required for object type 'NotSureItem' due to the following errors:
- Property 'text' is missing from latest object model.
- Property 'title' has been added to latest object model.
- Property 'textDescription' has been added to latest object model.'
这是我的代码:
import Foundation
import Realm
class NotSureItem: RLMObject {
dynamic var title = "" // renamed from 'text'
dynamic var textDescription = "" // added afterwards
dynamic var dateTime = NSDate()
}
只要您还没有发布您的应用程序,您只需删除您的应用程序,然后再运行。
每次更改 Realm 对象的属性时,现有数据库都会与新数据库不兼容。
只要您仍处于开发阶段,您只需从模拟器/设备中删除该应用程序,然后重新启动即可。
稍后当您的应用程序发布后并且您更改了对象的属性,您必须迁移到新的数据库版本。
要实际执行迁移,您需要实现一个 Realm 迁移块。通常,您会将块添加到 application(application:didFinishLaunchingWithOptions:)
:
var configuration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
// if just the name of your model's property changed you can do this
migration.renameProperty(onType: NotSureItem.className(), from: "text", to: "title")
// if you want to fill a new property with some values you have to enumerate
// the existing objects and set the new value
migration.enumerateObjects(ofType: NotSureItem.className()) { oldObject, newObject in
let text = oldObject!["text"] as! String
newObject!["textDescription"] = "The title is \(text)"
}
// if you added a new property or removed a property you don't
// have to do anything because Realm automatically detects that
}
}
)
Realm.Configuration.defaultConfiguration = configuration
// opening the Realm file now makes sure that the migration is performed
let realm = try! Realm()
每当您的方案发生变化时,您都必须在迁移块中增加 schemaVersion
并在块内更新所需的迁移。
下面的代码对我有用
- (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;
}
您修改后的数据库不再与保存的数据库兼容,因此需要进行迁移。您的选择是删除旧的数据库文件并重新开始(如果您处于初始开发阶段,效果很好),或者如果您是实时的,请进行迁移。
您可以通过定义架构版本并在您的领域配置中提供数据库迁移来完成此操作 'script'。整个过程记录在此处(连同代码示例):here
您可以像这样在启动时擦除数据库:
[[NSFileManager defaultManager] removeItemAtURL:[RLMRealmConfiguration defaultConfiguration].fileURL error:nil];
删除应用程序并重新安装不是一个好的做法。从我们第一次遇到迁移需求开始,我们就应该在开发过程中加入一些迁移步骤。 SilentDirge给出的link很好:realm migration document,给出了很好的例子来处理不同的情况。
对于最小的迁移任务,上面 link 中的以下代码片段可以自动进行迁移,并与 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()
只需增加架构版本
Realm 将自动检测新属性和删除的属性
var 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: 2,
// 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
}
})
do{
realm = try Realm(configuration: config)
print("Database Path : \(config.fileURL!)")
}catch{
print(error.localizedDescription)
}
如果您在增加 schemaVersion 后仍收到此错误。然后做一个double-check。 您是否在更新 App Delegate 中的模式版本之前调用任何 Realm 对象。
在我的例子中,我试图在执行该代码迁移语句之前访问 App Delegate 中的 Realm 对象。
始终将迁移代码写在App Delegate(DidfinishLaunchingWithOptions)的第一行,以确保安全。
我有一个对象 NotSureItem
,其中我有三个属性 title
,其名称是从我后来添加的 text
和 textDescription
重命名的,还有一个 dateTime
属性。现在,当我要去 运行 我的应用程序时,当我想向这些属性添加一些东西时它崩溃了。它显示以下语句。
'Migration is required for object type 'NotSureItem' due to the following errors:
- Property 'text' is missing from latest object model.
- Property 'title' has been added to latest object model.
- Property 'textDescription' has been added to latest object model.'
这是我的代码:
import Foundation
import Realm
class NotSureItem: RLMObject {
dynamic var title = "" // renamed from 'text'
dynamic var textDescription = "" // added afterwards
dynamic var dateTime = NSDate()
}
只要您还没有发布您的应用程序,您只需删除您的应用程序,然后再运行。
每次更改 Realm 对象的属性时,现有数据库都会与新数据库不兼容。
只要您仍处于开发阶段,您只需从模拟器/设备中删除该应用程序,然后重新启动即可。
稍后当您的应用程序发布后并且您更改了对象的属性,您必须迁移到新的数据库版本。
要实际执行迁移,您需要实现一个 Realm 迁移块。通常,您会将块添加到 application(application:didFinishLaunchingWithOptions:)
:
var configuration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
// if just the name of your model's property changed you can do this
migration.renameProperty(onType: NotSureItem.className(), from: "text", to: "title")
// if you want to fill a new property with some values you have to enumerate
// the existing objects and set the new value
migration.enumerateObjects(ofType: NotSureItem.className()) { oldObject, newObject in
let text = oldObject!["text"] as! String
newObject!["textDescription"] = "The title is \(text)"
}
// if you added a new property or removed a property you don't
// have to do anything because Realm automatically detects that
}
}
)
Realm.Configuration.defaultConfiguration = configuration
// opening the Realm file now makes sure that the migration is performed
let realm = try! Realm()
每当您的方案发生变化时,您都必须在迁移块中增加 schemaVersion
并在块内更新所需的迁移。
下面的代码对我有用
- (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;
}
您修改后的数据库不再与保存的数据库兼容,因此需要进行迁移。您的选择是删除旧的数据库文件并重新开始(如果您处于初始开发阶段,效果很好),或者如果您是实时的,请进行迁移。
您可以通过定义架构版本并在您的领域配置中提供数据库迁移来完成此操作 'script'。整个过程记录在此处(连同代码示例):here
您可以像这样在启动时擦除数据库:
[[NSFileManager defaultManager] removeItemAtURL:[RLMRealmConfiguration defaultConfiguration].fileURL error:nil];
删除应用程序并重新安装不是一个好的做法。从我们第一次遇到迁移需求开始,我们就应该在开发过程中加入一些迁移步骤。 SilentDirge给出的link很好:realm migration document,给出了很好的例子来处理不同的情况。
对于最小的迁移任务,上面 link 中的以下代码片段可以自动进行迁移,并与 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()
只需增加架构版本
Realm 将自动检测新属性和删除的属性
var 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: 2,
// 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
}
})
do{
realm = try Realm(configuration: config)
print("Database Path : \(config.fileURL!)")
}catch{
print(error.localizedDescription)
}
如果您在增加 schemaVersion 后仍收到此错误。然后做一个double-check。 您是否在更新 App Delegate 中的模式版本之前调用任何 Realm 对象。
在我的例子中,我试图在执行该代码迁移语句之前访问 App Delegate 中的 Realm 对象。
始终将迁移代码写在App Delegate(DidfinishLaunchingWithOptions)的第一行,以确保安全。