无法获取核心数据持久存储的元数据

Unable to get metadata for Core Data Persistent Store

我正在使用由 Core Data 支持的 RESTKit。我正在尝试检测我是否有新的核心数据模型版本。我已将其设置为自动映射更改我只需要检测迁移以更新 UI。我的代码是:

NSError *error;
NSURL *sourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"]]
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:nil URL:sourceURL error:&error];
BOOL needsMigration = ![[self managedObjectModel] isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];

sourceMetadata 变量始终为 nil,我似乎无法弄清楚原因。错误信息对我帮助不大:

错误域=NSCocoaErrorDomain 代码=134000 "The operation couldn’t be completed. (Cocoa error 134000.)"

我哪里错了?

你的错误是sourceURL是你模型文件的路径:

NSURL *sourceURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"]]

Model.momd,在app bundle中。您需要使用持久存储文件的路径,可能 $SOMETHING.sqlite 并且可能在您应用程序的文档目录中。

错误 134000 是 NSPersistentStoreInvalidTypeError,Apple 将其描述为 "Error code to denote an unknown persistent store type/format/version"。这表明您的 URL 中的持久存储文件有问题,或者它可能丢失了。要做的第一件事是查看 error 并查看 NSPersistentStoreCoordinator 试图告诉您的内容。很有可能它会准确地告诉您出了什么问题,但您没有在听。

如果文件存在于 URL,它 可能 有助于将查找调用的第一个参数从 nil 更改为 NSSQLiteStoreType,或您正在使用的任何商店类型。