问题描述
您好,我正在设备中测试我的核心数据应用程序,但它在 iphone 中崩溃并且在模拟器中运行良好.这是我的代码..
Hello I am testing my core data application in device, but it is crashing in iphone and working fine in simulator.Here is my code..
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TouristGuide" withExtension:@"momd"];
managedObjectModel= [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSLog(@"%@", modelURL);
return managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"TouristGuide.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"TouristGuide" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
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;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
return persistentStoreCoordinator;
}
设备日志显示异常:
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
例外情况是无法使用 nil 模型创建 nspersistancecordinator
,其次是 cannot find Entity 'City'
.
The exceptions are cannot create nspersistancecordinator with a nil model
and second is cannot find Entity 'City'
.
我该如何解决这个问题?
How can I solve this problem?
崩溃日志
0x352d1de0 -[NSPersistentStoreCoordinator initWithManagedObjectModel:] + 252 10 TouristGuide
0x0000358e -[TouristGuideAppDelegate persistentStoreCoordinator] (TouristGuideAppDelegate.m:176) 11 TouristGuide
0x0000310e -[TouristGuideAppDelegate managedObjectContext] (TouristGuideAppDelegate.m:124) 12 TouristGuide
0x000041f6 -[CityViewController viewDidLoad] (CityViewController.m:43)
推荐答案
按住 option 键,进入 Product 菜单 (Xcode 4) 并执行Clean Build Folder"... 然后让关闭选项键,然后执行清洁"...
Hold down the option key, go to Product menu (Xcode 4) and do "Clean Build Folder"... then let off the option key, and do "Clean"...
现在执行产品菜单 > 分析并确保一切都符合 kosher.
Now do Product menu > Analyze and make sure all is kosher.
最后转到项目查看器的左侧面板,然后单击代表整个项目的图标.在主面板中出现的窗口中,在目标"下,单击您的应用程序.现在单击构建阶段"并展开复制捆绑资源".确保您的 TouristGuide.xcdatamodeld 文件在那里.接下来,展开Link Binary with Libraries"并确保CoreData.framework"在其中(如果您将 Core Data 添加到一个不是作为 Core Data 项目开始的现有项目,那么很可能它不在那里).接下来,展开编译源",当然,仔细检查所有模型类是否都在其中(您知道,NSManagedObject 自定义子类).
Lastly go to the left hand panel of the project viewer, and click on the icon that represents your entire project. In the window that comes up in the main panel, under "Targets," click on your app. Now click "Build Phases" and expand "Copy Bundle Resources." Make sure that your TouristGuide.xcdatamodeld file is there. Next, expand "Link Binary with Libraries" and make sure that "CoreData.framework" is in there (if you added Core Data to an existing project that didn't start out as a Core Data project, then chances are, it's not in there). Next, expand "Compile Sources" and, of course, double-check that all your model classes are in there (y'know, the NSManagedObject custom sub-classes).
说到那些模型类...现在单击您的 TouristGuide.xcdatamodeld 并单击默认"配置.确保在类"列中显示NSManagedObject"的任何类都没有自定义类,如果它们确实列出了自定义类,请确保您具有与它们对应的等效命名的 .h 和 .m 文件.
Speaking of those model classes... now click on your TouristGuide.xcdatamodeld and click on the "Default" configuration. Make sure any classes that say "NSManagedObject" in the "Class" column do NOT have custom classes, and if they do list a custom class, make sure you have the equivalently-named .h and .m files corresponding to those.
10-to-1 这与您的模型文件本身有关.有一次我更改了模型文件的完整路径(它应该是/Users/You/Documents/Developer/MyApp/MyModel.xcdatamodeld/MyModel.xcdatamodel).一旦我改变了它,我就想不出如何把它改回来,因为 MyModel.xcdatamodeld 是一个包"文件,而不是一个文件夹.当然,在查找器中,您可以执行显示包内容...",但是 XCode 用于选择文件位置的对话框拒绝导航进入包内容!所以我无法重新选择位于 .xcdatamodeld 文件中的 .xcdatamodel 文件!我不得不从头开始完全重新创建我的整个模型文件,这是一个巨大的痛苦.也许有办法做到这一点,但我想不通.
10-to-1 it's something to do with your model file itself. Onetime I changed my model file's full path (it should be /Users/You/Documents/Developer/MyApp/MyModel.xcdatamodeld/MyModel.xcdatamodel). Once I had changed it, I couldn't figure out how to change it back, because MyModel.xcdatamodeld is a "package" file, not a folder. Sure, in the finder, you can do "Show Package Contents...", but XCode's dialog box for picking the file's location refused to navigate into the package contents! SO I was unable to ever re-select the .xcdatamodel file that resides within the .xcdatamodeld file! I had to totally recreate my entire model file from scratch, it was a huge pain. Maybe there's a way to do it but I couldn't figure it out.
还有最后两个技巧.
最后一个技巧,为了以防万一,在你的项目的备份上试试这个,但当我卡住时它对我有用:改变你的代码:
First last trick, try this on a backup of your project just in case, but it worked for me when I got stuck: change your code from this:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TouristGuide" withExtension:@"momd"];
为此:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TouristGuide" withExtension:@"mom"];
...将妈妈"更改为妈妈".
...change "momd" to "mom".
然后从设备或模拟器中删除应用程序,清理构建文件夹",清理",然后重建并重新运行项目.它可能会给你错误,也可能不会.之后,将其改回momd"并重做所有这些步骤.更改此设置可能会使 XCode 删除的内容比执行清理构建文件夹"并重新安装应用程序时要删除的内容多.这样,当您切换回来时,它会创建一个新的momd"文件.
Then delete the app from device or simulator, "Clean build folder," "clean," and rebuild and re-run the project. It might give you errors, it might not. After that, then change it back to "momd" and redo all those steps. Changing this will might make XCode delete more stuff than it otherwise would when you do "Clean Build Folder" and reinstall the app. That way it's making a fresh "momd" file when you switch back.
最后一招:
在你的标题中 #import 也没有坏处,只是为了祝你好运.:D
It also never hurts to #import in your headers, just for good luck. :D
希望这会有所帮助.如果有人在一开始就告诉我这些东西,我会节省很多时间.
Hope this helps. If someone would have told me this stuff at the beginning it would have saved me a lot of time.
这篇关于核心数据应用程序在 iphone 设备中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!