kid*_*d_x 8 database model core-data version ios
简短的问题:
我想在我的核心数据模型发生变化(新实体,新属性等)时在我的应用程序中运行某些代码.如何确定模型是否已更改?
只是一些伪代码:
if (current_model_version != previous_model_version) {
//do some code
} else {
// do some other code
}
Run Code Online (Sandbox Code Playgroud)
我猜我可能会使用versionHashes来执行此操作,或者isConfiguration:compatibleWithStoreMetadata:,但我不确定如何操作.
为清晰起见进行了一些编辑:"现在"和"上一次"中的"当前"和"上一次启动应用程序"一样.
答案似乎是isConfiguration:compatibleWithStoreMedia : .
我在这里找到了一些有用的信息
http://mipostel.com/index.php/home/70-core-data-migration-standard-migration-part-2
我这样设置:
- (BOOL)modelChanged
{
NSError *error;
NSURL * sourceURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"db.sqlite"];
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:sourceURL error:&error];
BOOL isCompatible = [[self managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
return isCompatible;
}
Run Code Online (Sandbox Code Playgroud)
'self'是我的共享数据存储,而不是必须去那里.
deanWombourne指出,真正做的是确定数据是否可以自动迁移,因此它并不是我提出的问题的解决方案.在这种情况下,它确实满足了我的需求.