【问题标题】:"Can't find model for source store" using Core Data lightweight migration and UIManagedDocument使用 Core Data 轻量级迁移和 UIManagedDocument 的“找不到源存储模型”
【发布时间】:2015-08-13 14:16:06
【问题描述】:

我正在尝试在现有且非常简单的核心数据模型的实体上简单地添加一个新属性......这是一场噩梦。

我创建了模型的新版本并在新版本中添加了我的新属性。


然后我尝试运行(在 iPhone 6 上,因为你知道......模拟器)。 没有崩溃,而是漂亮的以下错误:

2015-08-13 15:27:47.963 MyApp[15605:6736100] CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Containers/Data/Application/3FF51856-ECA5-4179-AA2A-FBE5EBFDDFD2/Documents/chat.db/StoreContent/persistentStore options:{
    NSInferMappingModelAutomaticallyOption = 1;
    NSMigratePersistentStoresAutomaticallyOption = 1;
    NSPersistentStoreRemoveStoreOnCleanupKey = 1;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0x176c61600 {URL=file:///var/mobile/Containers/Data/Application/3FF51856-ECA5-4179-AA2A-FBE5EBFDDFD2/Documents/chat.db/StoreContent/persistentStore, metadata={
    NSPersistenceFrameworkVersion = 519;
    NSStoreModelVersionHashes =     {
        Conversation = <e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158>;
        ConversationBinaryMessage = <dad3bd33 9cb61f03 f2895527 fa4a0b68 998bd8f3 d8196097 a0a8ccf0 e6076053>;
        ConversationLocationMessage = <6efd4294 b70e3110 cea51fd8 89471130 823cd964 0d7cdcd9 cdbc21e6 d995de0a>;
        ConversationMessage = <11bb9596 38fa5770 d5ace0c6 063da45e 3afa77fe d5d0f012 ba94dca3 2a61fb19>;
        ConversationStickerMessage = <be1d0d79 568c2ce5 ad9654d4 51dda203 9bc28162 8e50c9df 3d8b9d04 00d12294>;
        ConversationTextMessage = <7e79e7fd 32e5ef72 b98e544b 36073978 abf202ca fc78620b d200ca02 00a33181>;
        ...
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "BB890469-0382-4C74-8D6E-4708607395D8";
    "_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store} with userInfo dictionary {
    URL = "file:///var/mobile/Containers/Data/Application/3FF51856-ECA5-4179-AA2A-FBE5EBFDDFD2/Documents/chat.db/StoreContent/persistentStore";
    metadata =     {
        NSPersistenceFrameworkVersion = 519;
        NSStoreModelVersionHashes =         {
            Conversation = <e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158>;
            ConversationBinaryMessage = <dad3bd33 9cb61f03 f2895527 fa4a0b68 998bd8f3 d8196097 a0a8ccf0 e6076053>;
            ConversationLocationMessage = <6efd4294 b70e3110 cea51fd8 89471130 823cd964 0d7cdcd9 cdbc21e6 d995de0a>;
            ConversationMessage = <11bb9596 38fa5770 d5ace0c6 063da45e 3afa77fe d5d0f012 ba94dca3 2a61fb19>;
            ConversationStickerMessage = <be1d0d79 568c2ce5 ad9654d4 51dda203 9bc28162 8e50c9df 3d8b9d04 00d12294>;
            ConversationTextMessage = <7e79e7fd 32e5ef72 b98e544b 36073978 abf202ca fc78620b d200ca02 00a33181>;
            ...
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
            ""
        );
        NSStoreType = SQLite;
        NSStoreUUID = "BB890469-0382-4C74-8D6E-2015-08-13 15:27:47.978 MyApp[15605:6736100] CoreData: annotation: NSPersistentStoreCoordinator's current model hashes are {
    Conversation = <18f45c07 25844dcb 9c392f93 5b108dd5 1aa8bcb7 ac596cda 65d09e4e e1e1e85c>;
    ConversationBinaryMessage = <dad3bd33 9cb61f03 f2895527 fa4a0b68 998bd8f3 d8196097 a0a8ccf0 e6076053>;
    ConversationLocationMessage = <6efd4294 b70e3110 cea51fd8 89471130 823cd964 0d7cdcd9 cdbc21e6 d995de0a>;
    ConversationMessage = <11bb9596 38fa5770 d5ace0c6 063da45e 3afa77fe d5d0f012 ba94dca3 2a61fb19>;
    ConversationStickerMessage = <be1d0d79 568c2ce5 ad9654d4 51dda203 9bc28162 8e50c9df 3d8b9d04 00d12294>;
    ConversationTextMessage = <7e79e7fd 32e5ef72 b98e544b 36073978 abf202ca fc78620b d200ca02 00a33181>;
}

所以Conversation = &lt;e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158&gt;;

变成Conversation = &lt;18f45c07 25844dcb 9c392f93 5b108dd5 1aa8bcb7 ac596cda 65d09e4e e1e1e85c&gt;;

这是意料之中的。


这是我初始化文档的方式:

+ (UIManagedDocument *)sharedManagedDocumentForChat {
    static UIManagedDocument *managedDocument;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        NSFileManager *fileManager         = [NSFileManager defaultManager];
        NSArray       *documentDirectories = [fileManager URLsForDirectory:NSDocumentDirectory
                                                                 inDomains:NSUserDomainMask];

        if (documentDirectories.count) {
            NSURL *url = [documentDirectories[0] URLByAppendingPathComponent:@"chat.db"];
            managedDocument = [[UIManagedDocument alloc] initWithFileURL:url];
            managedDocument.persistentStoreOptions = @{ NSMigratePersistentStoresAutomaticallyOption:   @YES,
                                                        NSInferMappingModelAutomaticallyOption:         @YES };
        }
    });

    return managedDocument;
}

  • 我在这里缺少什么?
  • 为什么 Core Data 找不到源存储的模型?

感谢任何提示。


P.S:我有 2 个不同的 Core Data 数据库,1 个用于聊天功能,1 个用于缓存服务器响应,这会导致问题吗?


编辑

这里有两个模型以防万一。

【问题讨论】:

  • 嗨,我有类似的问题。你设法解决了你的问题吗?
  • 嗨@BojanBozovic,很遗憾,没有,我从未解决过这个问题,不得不寻找另一种方法来检索不涉及Core Data的信息。

标签: ios objective-c core-data database-migration


【解决方案1】:

您的项目中是否还有原始(版本 1)模型文件?

每次修改模型文件时都需要制作一份副本。

苹果文档

要执行自动迁移,Core Data 必须能够在运行时自己找到源和目标托管对象模型(请参阅 Core Data 必须能够推断映射)。如果您需要将模型放在自动发现未检查的位置,则需要生成推断模型并使用迁移管理器(NSMigrationManager 的实例)自己启动迁移。

【讨论】:

  • 是的,正如您在我的问题描述中看到的那样,我现在拥有chat.xcdatamodeld,其中包含新版本chat 2.xcdatamodel 和以前的版本chat.xcdatamodel
猜你喜欢
  • 2011-04-30
  • 2014-07-07
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-22
  • 2014-10-06
相关资源
最近更新 更多