0

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];
}
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
PonyLand
  • 115
  • 2
  • 10

1 Answers1

3

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.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
  • - (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]; – PonyLand Dec 03 '11 at 12:28
  • - (NSURL *)applicationDocumentsDirectory { return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; } – PonyLand Dec 03 '11 at 12:57
  • URLByAppendingPathComponent is "available in iOS 4 and later": http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html – matt Dec 03 '11 at 19:26
  • There is no such thing as FALSE, you probably mean NO – matt Dec 03 '11 at 19:28
  • @matt see http://stackoverflow.com/questions/615702/is-there-a-difference-between-yes-no-true-false-and-true-false-in-objective-c NO is more commonly used in Objective C but it is misleading to say there is no such thing as FALSE. – Duncan Babbage Dec 03 '11 at 19:30
  • 1
    Cool, I'll look in the headers before I post if you'll look in the class docs before you post. :) – matt Dec 03 '11 at 19:35
  • Yes, trust me to rely on an out of date SO answer and that comment deserves my up vote! However, I deleted it before you posted, at least from my browser's perspective. :) – Duncan Babbage Dec 03 '11 at 20:00
  • Removed the abort(); and the app launches but it doesn't load the database; it just doesn't find it. By the way, I installed the app via ssh. – PonyLand Dec 04 '11 at 00:10
  • The abort() is there for a reason—the app exits when it is unsuccessful at setting up the persistent store, because it knows that means the app won't be able to function correctly. That's the function of the abort(). – Duncan Babbage Dec 04 '11 at 00:28
  • @PonyLand, you are not listening to what Duncan is telling you. We *know* what's wrong here; the NSPersistentStoreCoordinator isn't able to get itself going, because it isn't finding the stuff it needs where you're telling it to look. Duncan has made clear the possible sources of error. – matt Dec 04 '11 at 02:38