我正在向一个模型中添加新的属性,但是一些领域错误使我感到困惑。我尝试的第一件事是将regDate属性(NSString)更改为NSDate类型。
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];我还改变了Track.h。
@property NSString *regDate;变成@property NSDate *regDate;
但我遇到了这样的运行时错误
*** Terminating app due to uncaught exception 'RLMException',
reason: 'Migration is required due to the following errors:
- Property 'Track.regDate' has been changed from 'string' to 'date'.原因是迁移是必需的。但是,从未执行过迁移块。
我认为除了从旧对象创建新属性并接受这个无用的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"]];
}];
}
};并添加@property NSDate *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.这次迁移块也不起作用。
我好像漏掉了什么。文档和错误无助于掌握正在发生的事情。
发布于 2018-05-13 13:34:39
我通过将领域3.1.1更新到3.5.0来解决问题
我不知道这是不是个窃听器。但是,如果有人使用3.1.1版本,我建议进行更新。
https://stackoverflow.com/questions/50314390
复制相似问题