问题描述
简短问题:
仅当我的核心数据模型已更改(新实体,新属性等)时,我才想在我的应用中运行特定代码.如何确定模型是否已更改?
I want to run a certain code in my app only if my Core Data model has changed (new entities, new properties, etc). How can I determine if the model has changed or not?
只需一些伪代码:
if (current_model_version != previous_model_version) {
//do some code
} else {
// do some other code
}
我猜我可能会使用versionHashes或isConfiguration:compatibleWithStoreMetadata:来做到这一点,但我不确定如何做到.
I'm guessing I might use versionHashes to do this, or isConfiguration:compatibleWithStoreMetadata:, but I'm not certain how.
为清晰起见,需要进行一些现在"为当前",而上次应用程序启动"为上一个".
Some editing for clarity: 'current' as in 'now' and 'previous' as in 'last time app was launched.'
推荐答案
答案似乎是 isConfiguration:compatibleWithStoreMedia:.
我在这里找到了一些有用的信息:
I found some useful information here:
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;
}
'self'是我的共享数据存储,而不必一定要去那里.
'self' is my shared data store, not that it necessarily has to go there.
deanWombourne指出,这实际上是确定是否可以自动迁移数据,因此这并不是我提出的问题的解决方案.在这种情况下,它确实满足了我的需求.
deanWombourne points out that what this really does is determine whether or not the data can be automatically migrated, so it's not exactly the solution to the problem I posed. It does serve my needs in this case.
这篇关于确定核心数据模型中何时有新版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!