0
votes

I am working on a core data project that will communicate with my Ipad and send core data back and forth. I have the network portion mostly figured out but I'm having issues with getting a simple fetch request to behave. Here is the fetch request:

NSManagedObjectContext *context=[[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];
//NSManagedObjectContext *context=[self managedObjectContext];
if (context == nil){
    NSLog(@"Crap");
}
NSLog(@"Context:  %@",context);
//fetch request:  (found here:  http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Articles/05_Fetching.html)
NSLog(@"Starting to fetch:");

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Song" inManagedObjectContext:context];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"cueNo" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
NSError *error;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];

If I use this:

NSManagedObjectContext *context=[[[NSDocumentController sharedDocumentController] currentDocument] managedObjectContext];

Then I get an error that says:

+entityForName: could not locate an NSManagedObjectModel for entity name 'Song'

If I use this:

NSManagedObjectContext *context=[self managedObjectContext];

mutableFetchResults comes back null even though I have data in the entity Song.

I copied this fetch request directly from the previous version utilizing the first method to set the context and it worked fine there.

Any help would be greatly appreciated!

Troubleshooting:

Printing description of persistentStoreCoordinator:

    Printing description of _managedObjectModel:
(<NSManagedObjectModel: 0x1001c5250>) isEditable 0, entities {
    Song = "(<NSEntityDescription: 0x100149ba0>) name Song, managedObjectClassName NSManagedObject, renamingIdentifier Song, isAbstract 0, superentity name (null), properties {\n    cueName = \"(<NSAttributeDescription: 0x1001c5600>), name cueName, isOptional 1, isTransient 0, entity Song, renamingIdentifier cueName, validation predicates (\\n), warnings (\\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n    cueNo = \"(<NSAttributeDescription: 0x1001c5570>), name cueNo, isOptional 1, isTransient 0, entity Song, renamingIdentifier cueNo, validation predicates (\\n), warnings (\\n), versionHashModifier (null), attributeType 700 , attributeValueClassName NSString, defaultValue (null)\";\n}, subentities {\n}, userInfo {\n}, versionHashModifier (null)";
}, fetch request templates {
    newFetchRequest = "<NSFetchRequest: 0x1001c5420> (entity: Song; predicate: (cueNo < \"0\"); sortDescriptors: (null); limit: 0)";
}

Printing description of _managedObjectContext:
<NSManagedObjectContext: 0x1001c5890>
2

2 Answers

0
votes

The error message is pretty clear, it tells you that NSEntityDescription couldn't find any entity with that name in the managed object context you provided. Since you appear to be verifying that the context is not nil (a good first step) you next need to consider the rest of the Core Data stack. Does the managed object context have a persistent store coordinator? Make sure. Does the persistent store coordinator have a managed object model? Does that model actually contain the Song entity? Somewhere along the line something is broken but there's not enough detail above to guess where.

As for the second managed object context option, executeFetchRequest:error: should only return nil if an error occurs. You're passing in an error parameter, so that's the first thing you should look at to determine what's wrong.

The fetch request is not obviously wrong, but your problems suggest that your managed object contexts are somehow broken.

0
votes

The error is telling you that the managed object context does not have managed object model that contains the entity Song. The managed object model is the .xcdatamodel file.

If you want to open a persistent store on another device, your local app must have access to the same model file that created remote persistent store and it must add the model to the local context. The error you are getting indicates this is not happening.

If this was working previously, then you've probably simply not added or have removed the .xcdatamodel file defining the Song entity from the local app's build target in Xcode.