ios 无法使用 nil 模型创建 NSPersistentStoreCoordinator
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7361187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot create an NSPersistentStoreCoordinator with a nil model
提问by Dominic Williams
Been having my first crack at Core Data and I'm getting the following error when running my code on my device, but it works fine on the simulator..
我第一次破解 Core Data 时,在我的设备上运行我的代码时出现以下错误,但它在模拟器上运行良好..
*Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法使用 nil 模型创建 NSPersistentStoreCoordinator”
Some of my methods that might be causing the problem:
我的一些可能导致问题的方法:
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created from the application's model.
*/
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"RugbyOnTv" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"RugbyOnTV.sqlite"];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
EDIT
编辑
I copied and pasted the managedObjectContext method (below) from Apple's CoreDataBooks and it now works..Not really sure why though
我从 Apple 的 CoreDataBooks 中复制并粘贴了 managedObjectContext 方法(如下),现在它可以工作了..不知道为什么
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
return managedObjectModel;
}
回答by Gareth Lloyd
I had exactly the same error message as the original post. I was wrestling with this for hours. It was this line in my AppDelegate.m.
我有与原始帖子完全相同的错误消息。我为此纠结了好几个小时。这是我 AppDelegate.m 中的这一行。
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"[same with name of xcdatamodeld]" withExtension:@"momd"];
For anyone out there searching this error message and finding this thread....try this first.
对于在那里搜索此错误消息并找到此线程的任何人....先试试这个。
You must make sure that where it says [same with name of xcdatamodeld]....that it is!!! For some reason, mine had my project name in there and not the data model name.
你必须确保它说 [与 xcdatamodeld 的名称相同]......它是!出于某种原因,我的项目名称在那里,而不是数据模型名称。
Changed it and it worked straight away.....
改变它,它立即起作用......
Thanks to Rock & Muller for contributing.......you saved me days!!
感谢 Rock & Muller 的贡献......你为我节省了很多时间!!
Gaz.
盖兹。
回答by D33pN16h7
first verify:
首先验证:
NSLog(@"%@", [self managedObjectModel]);
If you get a nil value maybe the problem is here
如果你得到一个 nil 值,也许问题就在这里
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"RugbyOnTv" withExtension:@"momd"];
So, try changing @"momd" by @"mom"
所以,试着用@"mom" 改变@"momd"
回答by xster
I've experienced a weird issue with Xcode 4.3.2 and iOS 5.
我在 Xcode 4.3.2 和 iOS 5 上遇到了一个奇怪的问题。
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAME_OF_THE_MODEL" withExtension:@"momd"];
returns a valid URL but
返回一个有效的 URL 但
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
returns a null NSManagedObjectModel. But after checking the documentation, it seems like NSManagedObjectModel needs a file where as NAME_OF_THE_MODEL.momd is a directory which contains a NAME_OF_THE_MODEL.mom file. Changing the URL to
返回一个空的 NSManagedObjectModel。但是在检查文档之后,似乎 NSManagedObjectModel 需要一个文件,其中 NAME_OF_THE_MODEL.momd 是一个包含 NAME_OF_THE_MODEL.mom 文件的目录。将网址更改为
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"NAME_OF_THE_MODEL" withExtension:@"mom" subdirectory:@"NAME_OF_THE_MODEL.momd"];
then works. Seems weird though that Xcode generates code that doesn't work with itself...
然后工作。尽管 Xcode 生成的代码与自身无关,但这似乎很奇怪......
回答by JDx
I had this problem and changing "moms" to "mom" didn't do anything. To fix it I had to right click on the xcdatamodelId file > show package contents then delete the hidden .xcurrentversion file.
我遇到了这个问题,将“妈妈”更改为“妈妈”并没有做任何事情。要修复它,我必须右键单击 xcdatamodelId 文件 > 显示包内容,然后删除隐藏的 .xcurrentversion 文件。
P.S: This problem only started happening after I'd changed the name of the data model file.
PS:这个问题只在我更改数据模型文件的名称后才开始发生。
回答by Dave Wood
Another source of this error, is that sometimes Xcode doesn't include the data model in the build.
此错误的另一个来源是有时 Xcode 在构建中不包含数据模型。
Check the Build Phases of your target, and ensure that the *.xcdatamodeld file is included in the Compile Sources section.
检查目标的构建阶段,并确保 *.xcdatamodeld 文件包含在编译源部分中。
回答by Louis Cremen
What's probably happened is that your xcdatamodeld file from Apple's source code has turned into a xcdatamodel(without the d) file and therefore they aren't considered the same.
可能发生的情况是,来自 Apple 源代码的 xcdatamodeld 文件已变成 xcdatamodel(没有 d)文件,因此它们不被视为相同。
The quickest way to fix this, is to select the xcdatamodel file in your project navigator and in to the menu bar
解决此问题的最快方法是在项目导航器和菜单栏中选择 xcdatamodel 文件
Editor->Add Model Version...
Editor->Add Model Version...
and add a new version of your model. Make all changes to the new version.
并添加模型的新版本。对新版本进行所有更改。
This works in Xcode 5
这适用于 Xcode 5
回答by smileBot
I solve it by adding the db file to the Copy Bundle Resources
.
我通过将 db 文件添加到Copy Bundle Resources
.
Go to root of your project >> select your target >> Build Phases >> Copy Bundle Resources . Make sure your xcdatamodeld file has been added here.
转到项目的根目录 >> 选择您的目标 >> 构建阶段 >> 复制捆绑资源。确保您的 xcdatamodeld 文件已添加到此处。
None of the posted solutions worked for me. So, hopefully this post helps someone out there. My app only crashes in release builds btw. Lesson learned. Always test release builds!
没有一个已发布的解决方案对我有用。所以,希望这篇文章可以帮助那里的人。顺便说一句,我的应用程序仅在发布版本中崩溃。学过的知识。始终测试发布版本!
BTW, I found this SO post the most helpful https://stackoverflow.com/a/6708837/951349.
顺便说一句,我发现这篇文章最有帮助https://stackoverflow.com/a/6708837/951349。
回答by rock
i got same issue with @Dominic Williams
我和@Dominic Williams 有同样的问题
try to change the momd file name in below(you can find this in managedObjectModel
method in default), which as same as the [file name].xcdatamodeld
file you created:
尝试更改下面的 momd 文件名(您可以managedObjectModel
在默认方法中找到它),这与[file name].xcdatamodeld
您创建的文件相同:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"[same with name of xcdatamodeld]" withExtension:@"momd"];
回答by Salvador Ponticelli
I had the same problem. The solution was a mix of 2 answers:
我有同样的问题。解决方案是 2 个答案的混合:
1) I had to add the "subdirectory" parameter into the URLForResource call
1)我必须将“子目录”参数添加到 URLForResource 调用中
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DATAMODEL_NAME" withExtension:@"mom" subdirectory:@"DATAMODEL_NAME.momd"];
2) For a reason I don't know, the data model was not included when the project was compiled. I had to add it manually into the "Build Phases / Compile resources".
2)出于某种我不知道的原因,编译项目时未包含数据模型。我不得不手动将它添加到“构建阶段/编译资源”中。
With only one of the above solutions, my application didn't work.
只有上述解决方案之一,我的应用程序不起作用。
回答by XCool
I tried all the solutions here, and none of them worked. My issue appeared after I renamed the project. Apparently, Xcode continues to look for the old momd file in the wrong place during compilation.
我在这里尝试了所有解决方案,但没有一个奏效。我重命名项目后出现了我的问题。显然,Xcode 在编译期间继续在错误的位置寻找旧的 momd 文件。
For anyone that tried all the above solutions without success, try checking the full path of your .xcdatamodeld
file. That's what worked for me.
对于尝试上述所有解决方案但没有成功的任何人,请尝试检查.xcdatamodeld
文件的完整路径。这就是对我有用的东西。