本文介绍了核心数据应用程序崩溃在iphone设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

- (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:@TouristGuidewithExtension:@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(![fileManager fileExistsAtPath:storePath]){
NSString * defaultStorePath = [[NSBundle mainBundle] pathForResource:@TouristGuideofType:@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]){
//更新以适当地处理错误。
NSLog(@未解析的错误%@,%@,错误,[错误userInfo]);
exit(-1); // Fail
}


return persistentStoreCoordinator;
}

设备日志显示异常行:

  persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 

例外是无法使用nil模型创建nspersistancecordinator 第二个是找不到Entity'City'



我如何解决这个问题? >

崩溃日志

  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] m:43)


解决方案

按住选项键到产品菜单(Xcode 4)并执行清理构建文件夹...然后放开选项键,并执行清理...



现在进行产品菜单>分析并确保所有都是kosher。



最后转到项目查看器的左侧面板,然后单击表示整个项目的图标。在主面板中的目标下的窗口中,点击您的应用。现在点击Build Phases并展开Copy Bundle Resources。确保您的TouristGuide.xcdatamodeld文件存在。接下来,展开链接二进制库与库,并确保CoreData.framework在那里(如果您添加Core Data到一个现有的项目,并没有开始作为一个核心数据项目,那么机会,它不是那里)。接下来,展开编译源代码,当然,再次检查所有的模型类都在那里(你知道,NSManagedObject自定义子类)。



说到那些模型类...现在点击你的TouristGuide.xcdatamodeld并点击默认配置。确保在类列中说NSManagedObject的任何类都不具有自定义类,如果它们列出了自定义类,请确保您具有与之对应的等效命名的.h和.m文件。



10对1它与你的模型文件本身有关。一次我改变了我的模型文件的完整路径(它应该是/Users/You/Documents/Developer/MyApp/MyModel.xcdatamodeld/MyModel.xcdatamodel)。一旦我改变它,我不知道如何改变它,因为MyModel.xcdatamodeld是一个包文件,而不是一个文件夹。当然,在finder中,你可以做显示包内容...,但XCode的对话框用于选择文件的位置拒绝导航到包内容!所以我无法重新选择.xcdatamodeld文件驻留在.xcdatamodeld文件中的.xcdatamodel文件!我不得不从头开始完全重建我的整个模型文件,这是一个巨大的痛苦。



还有两个最后的窍门。



第一个最后一个窍门,试试这个对你的项目的备份,以防万一,但它对我工作的时候,我陷入:更改您的代码从这:

  NSURL * modelURL = [[NSBundle mainBundle] URLForResource:@TouristGuidewithExtension:@momd]; 

  NSURL * modelURL = [[NSBundle mainBundle] URLForResource:@TouristGuidewithExtension:@mom]; 

...将momd改为mom。



然后从设备或模拟器中删除应用程序,清理构建文件夹,清理,重建并重新运行项目。它可能会给你错误,它可能不会。之后,将其改回momd,重做所有这些步骤。改变这可能会使XCode删除更多的东西,否则当你做清洁构建文件夹,并重新安装应用程序。这样,当你切换回来时,它会创建一个新的妈妈文件。



第二个诀窍:



它也不会伤害#import在你的标题,只是为了好运。 :D



希望这有帮助。如果有人会在开始时告诉我这些东西,这会节省我很多时间。


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;
}

device log is showing exception in line:

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

The exceptions are cannot create nspersistancecordinator with a nil model and second is cannot find Entity 'City'.

How can I solve this problem?

Crash log

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)
解决方案

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"...

Now do Product menu > Analyze and make sure all is kosher.

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).

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 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.

Also, two last tricks.

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"];

to this:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TouristGuide" withExtension:@"mom"];

...change "momd" to "mom".

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.

Second last trick:

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设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:20