Ios 领域迁移失败:“;“需要迁移”;
我正在向模型添加新属性,但一些领域错误使我感到困惑。 我尝试的第一件事是将regDate属性(NSString)更改为NSDate类型Ios 领域迁移失败:“;“需要迁移”;,ios,objective-c,realm,realm-migration,Ios,Objective C,Realm,Realm Migration,我正在向模型添加新属性,但一些领域错误使我感到困惑。 我尝试的第一件事是将regDate属性(NSString)更改为NSDate类型 RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; config.schemaVersion = 2; config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion)
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 2;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
NSLog(@"========== Migration executed ==========");
if (oldSchemaVersion < 2) {
[migration enumerateObjects:Track.className block:^(RLMObject *oldObject, RLMObject *newObject) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
newObject[@"regDate"] = [dateFormatter dateFromString:oldObject[@"regDate"]];
}];
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];
[RLMRealm defaultRealm];
原因是需要迁移。但是,从未执行过迁移块
我认为除了从旧对象创建新属性并接受这个无用的regDate属性之外,别无选择
所以我改变了migrationBlock:
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
if (oldSchemaVersion < 2) {
// Note: Even if you don't have to convert placeholder values,
// you still have to provide at least an empty migration block
// when your schema has changes to nullability of properties.
[migration enumerateObjects:Track.className block:^(RLMObject *oldObject, RLMObject *newObject) {
NSLog(@"========== Migration executed ==========");
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
newObject[@"n_regDate"] = [dateFormatter dateFromString:oldObject[@"n_regDate"]];
}];
}
};
这次迁移块也不起作用
我好像错过了什么。文档和错误对理解正在发生的事情没有帮助。我通过将领域3.1.1更新为3.5.0解决了这个问题
我不知道这是否是一个错误。但是,如果有人正在使用3.1.1版,我建议进行更新。这段代码是否在AppDelegate的
ApplicationIDFinishLaunching
函数中调用?您何时收到迁移错误,是否在第[RLMRealm defaultRealm]行代码>?@DávidPászor首先,是的。第二,是的。你知道吗?那么我想到的另一个可能的原因是你没有更新schemaVersion
。你确定它不应该是3而不是2吗?我再次检查,我确定schemaVersion
是2,oldSchemaVersion
是1。所以迁移块被执行了?或者你怎么知道oldSchemaVersion
是1?
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
if (oldSchemaVersion < 2) {
// Note: Even if you don't have to convert placeholder values,
// you still have to provide at least an empty migration block
// when your schema has changes to nullability of properties.
[migration enumerateObjects:Track.className block:^(RLMObject *oldObject, RLMObject *newObject) {
NSLog(@"========== Migration executed ==========");
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
newObject[@"n_regDate"] = [dateFormatter dateFromString:oldObject[@"n_regDate"]];
}];
}
};
*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required due to the following errors:
- Property 'Track.n_regDate' has been added.