"无法找到源存储的模型"使用Core Data轻量级迁移和UIManagedDocument

时间:2015-08-13 14:16:06

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

我试图在现有且非常简单的Core Data模型的实体上添加一个新属性......这真是一场噩梦。

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

model versioning

然后我试着跑(在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 = <e884e2fd 37f94dc9 7039539a cdebcb76 8c11637f 5f5e1744 310ce768 6bdf9158>;

成为Conversation = <18f45c07 25844dcb 9c392f93 5b108dd5 1aa8bcb7 ac596cda 65d09e4e e1e1e85c>;

这是预期的。

以下是我初始化文档的方式:

+ (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;
}
  • 我在这里缺少什么?
  • 为什么核心数据无法找到源存储的模型?

感谢任何提示。

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

修改

以下是两种型号,以防万一。 models

1 个答案:

答案 0 :(得分:0)

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

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

Apple Docs

  

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