niklassaers
niklassaers

Reputation: 8830

Error loading my managedObjectModel

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

Upvotes: 1

Views: 1947

Answers (2)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46728

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.

Upvotes: 3

theMikeSwan
theMikeSwan

Reputation: 4729

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; 

}

Upvotes: 0

Related Questions