PonyLand
PonyLand

Reputation: 115

iPhone app crashes upon startup, don't understand why

My app uses an sqlite database managed with core-data. These are the errors I found in the debug files. On the simulator it works just fine, no problems at all. Made the app in Xcode 4.2 iOS 5 and tried it on an iPhone 4 4.2.1.

 Incident Identifier: 65E195D6-C583-4CA2-8017-1AEC56FAFBD4
    CrashReporter Key:   4ebf8de224465a40c94ea40cffb742f345888ef0
    Hardware Model:      iPhone3,1
    Process:         Jobs [158]
    Path:            /Applications/Jobs.app/Jobs
    Identifier:      Jobs
    Version:         ??? (???)
    Code Type:       ARM (Native)
    Parent Process:  punchd [1]    
    Date/Time:       2011-12-01 13:53:22.288 +0000
    OS Version:      iPhone OS 4.2.1 (8C148)
    Report Version:  104    
    Exception Type:  EXC_CRASH (SIGABRT)
    Exception Codes: 0x00000000, 0x00000000
    Crashed Thread:  0    
    Thread 0 Crashed:   
    4   Jobs                            0x00002c74 0x1000 + 7284
    5   Jobs                            0x00002a0a 0x1000 + 6666
    6   Jobs                            0x00003012 0x1000 + 8210   
    36  Jobs                            0x0000283c 0x1000 + 6204
    37  Jobs                            0x000027f4 0x1000 + 6132

Error 4 leads to this line of code:

__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(); //this one to be exact
    }    

Error 5 leads to this line of code:

- (NSManagedObjectContext *)managedObjectContext
{    if (__managedObjectContext != nil)
    {        return __managedObjectContext;    }    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; //this line with error;
    if (coordinator != nil)
    {        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];    }
    return __managedObjectContext;}

These were in the app delegate. Now the 6th error is from the main view, in the ViewDidLoad:

 if (managedObjectContext == nil) 
    { 
        managedObjectContext = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; //this line;
        NSLog(@" %@",  managedObjectContext);} 

In response to request in comments:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 
    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyDatabase.sqlite"];
    NSError *error = nil;
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
   ...

Also:

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Upvotes: 0

Views: 694

Answers (1)

Duncan Babbage
Duncan Babbage

Reputation: 20187

Your app is not crashing, it is aborting as you instruct it to in abort because this line of code is returning YES—as in, it didn't successfully addPersistentStoreWithType, since you are testing if(!:

 if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])

A good starting point to being able to solve this would be to show us the code that allocates/defines storeURL, options, and error. Most likely, one of these does not exist or has not been initialized correctly.

Upvotes: 3

Related Questions