Core Data;Cocoa错误134100

22
这是我第一次使用核心数据,出现了以下错误。如果您能告诉我如何解决,我将不胜感激。
Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x5927880 {metadata=<CFBasicHash 0x59269a0 [0x1007400]>{type = immutable dict, count = 7,
entries =>
    2 : <CFString 0x5926e10 [0x1007400]>{contents = "NSStoreModelVersionIdentifiers"} = <CFArray 0x5927240 [0x1007400]>{type = immutable, count = 0, values = ()}
    4 : <CFString 0x5927190 [0x1007400]>{contents = "NSPersistenceFrameworkVersion"} = <CFNumber 0x5926ca0 [0x1007400]>{value = +320, type = kCFNumberSInt64Type}
    6 : <CFString 0x59271c0 [0x1007400]>{contents = "NSStoreModelVersionHashes"} = <CFBasicHash 0x5927340 [0x1007400]>{type = immutable dict, count = 2,
entries =>
    0 : <CFString 0x5927280 [0x1007400]>{contents = "Details"} = <CFData 0x59272f0 [0x1007400]>{length = 32, capacity = 32, bytes = 0x434e180241ecf461e59580e640ff926b ... aa456d1410ed9d1b}
    2 : <CFString 0x5927260 [0x1007400]>{contents = "History"} = <CFData 0x59272a0 [0x1007400]>{length = 32, capacity = 32, bytes = 0x3437f77a5563363f66b9d72ea76e0ff1 ... be194eb9dd17cddc}
}

    7 : <CFString 0xe238b0 [0x1007400]>{contents = "NSStoreUUID"} = <CFString 0x5926fc0 [0x1007400]>{contents = "6C5FECCB-0B64-46EB-809B-E0A4577D1E90"}
    8 : <CFString 0xe23720 [0x1007400]>{contents = "NSStoreType"} = <CFString 0xe238f0 [0x1007400]>{contents = "SQLite"}
    9 : <CFString 0x59271f0 [0x1007400]>{contents = "NSStoreModelVersionHashesVersion"} = <CFNumber 0x4d13e20 [0x1007400]>{value = +3, type = kCFNumberSInt32Type}
    10 : <CFString 0x5927220 [0x1007400]>{contents = "_NSAutoVacuumLevel"} = <CFString 0x5927380 [0x1007400]>{contents = "2"}
}
, reason=The model used to open the store is incompatible with the one used to create the store}, {
    metadata =     {
        NSPersistenceFrameworkVersion = 320;
        NSStoreModelVersionHashes =         {
            Details = <434e1802 41ecf461 e59580e6 40ff926b 20004ce9 58eb965f aa456d14 10ed9d1b>;
            History = <3437f77a 5563363f 66b9d72e a76e0ff1 ed7f70fd cb7035e9 be194eb9 dd17cddc>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
        );
        NSStoreType = SQLite;
        NSStoreUUID = "6C5FECCB-0B64-46EB-809B-E0A4577D1E90";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
sharedlibrary apply-load-rules all
Current language:  auto; currently objective-c
kill
quit
Program ended with exit code: 0

My code is here: https://gist.github.com/898579

6个回答

65

这是原因:

用于打开存储的模型与用于创建存储的模型不兼容

以下是您的操作过程:

  1. 创建了一些带有属性的实体并编写了一些代码
  2. 启动应用程序,可能添加了一些内容
  3. 退出应用程序,并添加/更改了一些具有属性的实体
  4. 可能又重新启动了应用程序,现在它会给你报错

原因是因为您的新托管对象模型正在尝试使用旧版本的存储(即首次启动应用程序时创建的存储)。

快速且直接的解决方法是删除存储文件(位于~/Library/Application Support/YOUR_APP/),然后再次启动应用程序。

供以后参考 - 如果您发布了一个应用程序,并在下一个版本中更改了托管对象模型,则必须为其编写迁移。所有这些内容都在苹果文档中的核心数据编程食谱中涵盖。


2
我正在一个由两个人组成的团队中工作 - 当我们其中一人在已经被另一人安装了应用程序的手机上安装时,我们会遇到这个错误。数据模型/结构没有改变。这是否与签署应用程序的用户身份有关? - Marc
不知道那个。它可能会受到应用程序GUID的影响。 - Eimantas
奇怪 - 我们正在使用SVN作为版本控制。我担心如果另一个开发人员在我之后接手这个应用程序,他们可能会发布带有此崩溃的应用程序到应用商店。如果我找出原因,我会在这个线程中更新。 - Marc
太好了!感谢您的解决方案。我已经删除了旧数据库,现在一切都正常了! - Marcelo dos Santos

14

如果您从模拟器/设备中删除该应用程序,就足以解决问题。

您无需更改项目中的文件(除了选择当前模型并使用菜单替换使用类:Editor/Create NSManaged Object Subclass)。


1

您应该执行以下步骤:

  1. 如果仍然显示相同的错误,请删除应用程序并再次运行。这意味着您使用了不同类型的特定属性/属性。
  2. 转到您的.xcdatamodeled文件并检查每个属性的类型。
  3. 进入您的代码并检查,在将属性/对象插入核心数据时,您是否使用了不同的类型。因此,属性/对象在代码和.xcdatamodeled中的类型(NSString,NSDate等)应该相同。否则,它将给出错误“Error Domain=NSCocoaErrorDomain Code=134100”。

0

如果您处于开发模式并且不想每次更改实体时都删除应用程序,我建议使用此解决方案:

- (void)removeCoreDataStorage {
    // Where does the SQLite file go?
    NSArray *documentDirectories =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    // Get one and only document directory from that list
    NSString *documentDirectory = [documentDirectories firstObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"model.sqlite"];

    NSError *error = nil;
    NSURL *storeURL = [NSURL fileURLWithPath:path];

    NSURL *storeURLWal = [NSURL URLWithString:[storeURL.absoluteString stringByReplacingOccurrencesOfString:@".sqlite" withString:@".sqlite-wal"]];
    NSURL *storeURLShm = [NSURL URLWithString:[storeURL.absoluteString stringByReplacingOccurrencesOfString:@".sqlite" withString:@".sqlite-shm"]];

    BOOL isRemoveItemAtURL = [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
    if (isRemoveItemAtURL == NO) {
        NSLog(@"NO RemoveItemAtURL. Reason: %@", error.localizedFailureReason);
    }

    BOOL isRemoveItemAtURLWal = [[NSFileManager defaultManager] removeItemAtURL:storeURLWal error:&error];
    if (isRemoveItemAtURLWal == NO) {
        NSLog(@"NO RemoveItemAtURL. Reason: %@", error.localizedFailureReason);
    }

    BOOL isRemoveItemAtURLShm = [[NSFileManager defaultManager] removeItemAtURL:storeURLShm error:&error];
    if (isRemoveItemAtURLShm == NO) {
        NSLog(@"NO RemoveItemAtURL. Reason: %@", error.localizedFailureReason);
    }
}

别忘了用你自己的路径替换掉 path


问题出现在将iOS从10.3升级到10.3.2之后。这有助于解决问题。谢谢! - Shaked Sayag

0

我也遇到过这种情况,通过删除STORE URL并使用以下代码解决了问题 - [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

澄清: 如果我们不想在测试之间保留数据,则删除存储URL上的文件并重新创建它。由于新测试具有新的模型版本,旧文件无法正确打开。 此外,如果我们更改了托管对象模型-我们必须为其编写迁移。


0
有时候你需要从另一个应用程序创建的存储中加载数据 - 例如,一个应用程序仅用于从外部源加载数据并保存到存储中,而另一个应用程序使用这个充满数据的存储作为起点。
我不想说这是超级正确的,但如果您在应用程序开发过程中需要它,只需更改存储中的UUID(通过SQLVue等)为您的应用程序所期望的正确UUID。您将在表格中找到UUID,通常称为ZMETADATA,在唯一的一列中称为Z_UUID。但请记住 - 仅用于快速调试/开发目的。不要在编程中使用此功能。

我该如何以编程的方式实现这个功能,例如当我复制了一个UIManagedDocument并想要使用它的副本时? - AlexR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接