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>