【发布时间】:2017-07-14 10:37:44
【问题描述】:
假设我在一个架构更新中添加了一个属性 colorLenghtSort,并在下一版本中重命名该属性以删除错字:colorLengthSort。
我的迁移如下所示
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
config.schemaVersion = 25;
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
if(oldSchemaVersion < 24) {
// added Hairstyle.colorLenghtSort, will be added automatically
}
if(oldSchemaVersion < 25) {
// if (schema-has-property colorLenghtSort) .... <- how to get this?
[migration renamePropertyForClass:Hairstyle.className oldName:@"colorLenghtSort" newName:@"colorLengthSort"];
}
};
[RLMRealmConfiguration setDefaultConfiguration:config];
现在 如果用户来自 schemaVersion 23 或更低,则他的领域没有(拼写错误)属性,因为他跨越了 V24。然后,迁移将失败:
由于未捕获的异常“RLMException”而终止应用程序,原因: '不能重命名属性'Hairstyle.colorLenghtSort',因为它确实 不存在。'
如何查看该属性是否存在?
编辑:我找到了解决方案!
if(oldSchemaVersion < 25) {
RLMObjectSchema * oldhairstyleSchema = [[migration oldSchema] objectForKeyedSubscript:Hairstyle.className];
bool renameNecessary = NO;
for(RLMProperty * prop in oldhairstyleSchema.properties) {
if([prop.name isEqualToString:@"colorLenghtSort"]){
renameNecessary = YES;
break;
}
}
if(renameNecessary)
[migration renamePropertyForClass:Hairstyle.className oldName:@"colorLenghtSort" newName:@"colorLengthSort"];
}
【问题讨论】:
-
您应该发布您的编辑作为答案(然后接受它)。
标签: objective-c realm