确定核心数据模型何时有新版本

2024-01-15

简短的问题:

仅当我的核心数据模型发生更改(新实体、新属性等)时,我才想在我的应用程序中运行特定代码。如何确定模型是否已更改?

只是一些伪代码:

    if (current_model_version != previous_model_version) {
    //do some code
    } else {
    // do some other code
    }

我猜我可能会使用 versionHashes 来执行此操作,或者使用 isConfiguration:completeWithStoreMetadata: 来执行此操作,但我不确定如何执行此操作。

为了清楚起见,进行了一些编辑:“当前”如“现在”,“上一个”如“上次启动应用程序”。


答案似乎是isConfiguration:与StoreMedia兼容:.

我在这里找到了一些有用的信息:

http://mipostel.com/index.php/home/70-core-data-migration-standard-migration-part-2 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”是我的共享数据存储,不一定必须去那里。

DeanWombourne 指出,这实际上是确定数据是否可以自动迁移,因此这并不完全是我提出的问题的解决方案。在这种情况下它确实满足了我的需求。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

确定核心数据模型何时有新版本 的相关文章

随机推荐