views:

95

answers:

2

Hi guys,

When I call [myAppDelegate managedObjectModel], in the retain line below, my application will crash (iPhone SDK v3.1.3):

- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}

Here is my crash trace

#0  0x905c44e6 in objc_exception_throw
#1  0x01e78c3b in +[NSException raise:format:arguments:]
#2  0x01e78b9a in +[NSException raise:format:]
#3  0x000af99b in _NSArrayRaiseInsertNilException
#4  0x0001c360 in -[NSCFArray insertObject:atIndex:]
#5  0x0001c274 in -[NSCFArray addObject:]
#6  0x01c16a7e in +[NSManagedObjectModel mergedModelFromBundles:]
#7  0x00002432 in -[myAppDelegate managedObjectModel] at myAppDelegate.m:102

What is going on here? This is template code that I haven't seen fail before.

Cheers

Nik

+1  A: 

This is what I have as the default implementation:

- (NSManagedObjectContext *) managedObjectContext {

if (managedObjectContext != nil) {
    return managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return managedObjectContext; 

}

theMikeSwan
+2  A: 

What is the text that goes with that crash? You probably have duplicate entities or it is failing to find the model at all.

update

To be clear, the extension for a single file should be .xcdatamodel. .xcdatamodeld is meant for versioned bundles.

Marcus S. Zarra
Hi Marcus, you were very right, it's failing to find the model. My model extention was .xcdatamodel, not .xcdatamodeld. As soon as I switched to a .xcdatamodeld it was all working fine. :-)
niklassaers