我有一个对象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.'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import Foundation
import Realm
class NotSureItem: RLMObject {
dynamic var title = "" // renamed from 'text'
dynamic var textDescription = "" // added afterwards
dynamic var dateTime = NSDate()
}
Run Code Online (Sandbox Code Playgroud)
joe*_*ern 101
只要您尚未发布应用程序,就可以删除应用程序并再次运行.
每次更改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()
Run Code Online (Sandbox Code Playgroud)
每当您的方案发生更改时,您必须增加schemaVersion迁移块并更新块中所需的迁移.
Cod*_*rew 22
删除应用程序并重新安装不是一个好习惯.从我们第一次遇到迁移需求开始,我们应该在开发过程中加入一些迁移步骤.SilentDirge给出的链接很好:领域迁移文档,它为处理不同情况提供了很好的示例.
对于最小迁移任务,上述链接中的以下代码段可以自动执行迁移,并与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()Run Code Online (Sandbox Code Playgroud)
小智 10
下面的代码对我有用
- (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;
}
Run Code Online (Sandbox Code Playgroud)
更多信息:https://realm.io/docs/objc/latest/#getting-started
var config = Realm.Configuration(\n // Set the new schema version. This must be greater than the previously used\n // version (if you've never set a schema version before, the version is 0).\n schemaVersion: 2,\n \n // Set the block which will be called automatically when opening a Realm with\n // a schema version lower than the one set above\n migrationBlock: { migration, oldSchemaVersion in\n // We haven\xe2\x80\x99t migrated anything yet, so oldSchemaVersion == 0\n if (oldSchemaVersion < 1) {\n // Nothing to do!\n // Realm will automatically detect new properties and removed properties\n // And will update the schema on disk automatically\n }\n })\n \n \n do{\n realm = try Realm(configuration: config)\n \n print("Database Path : \\(config.fileURL!)")\n }catch{\n print(error.localizedDescription)\n }\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
20051 次 |
| 最近记录: |