So I'm working on a clone of CoreDataBooks. It's a little different. When the '+' button is pushed, it launches a navController, containing 2 views. The first (AddPatientVC) asks for name of the Patient then its pushed to a 2nd View Controller (AddPatientDetailVC) which asks for more detailed information. It's the 2nd view controller that I've got the delegate set up with, not the first, like in CoreDataBooks.
For some reason, when the delegate method is fired, the notification method doesn't get fired, so I've somehow lost track of my MOC, either the specific MOC for adding a new Patient.
The specific error i get is:'+entityForName: could not locate an NSManagedObjectModel for entity name 'Patient''
Here's my code - addPatient, delegate method and notification method. Any suggestions on simplification would be appreciated. Thanx
-(void)addPatient:(id)sender
{
PatientAddViewController *patientAddViewController = [[PatientAddViewController alloc] initWithNibName:@"PatientAddViewController" bundle:nil];
PatientAddDetailViewController *patientAddDetailViewController = [[PatientAddDetailViewController alloc] initWithNibName:@"PatientAddViewController" bundle:nil];
patientAddDetailViewController.delegate = self;
//Create a new MOC for adding a book
NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
self.addPatientManagedObjectContext = addingContext;
[addingContext release];
[addPatientManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
patientAddViewController.patient = (Patient *)[NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext:addingContext];
//patientAddViewController.addPatientManagedObjectContext = self.addPatientManagedObjectContext;
UINavigationController *addingNavController = [[UINavigationController alloc] initWithRootViewController:patientAddViewController];
[self.navigationController presentModalViewController:addingNavController animated:YES];
[addingNavController release];
[patientAddViewController release];
}
- (void)patientAddDetailViewController:(PatientAddDetailViewController *)controller didFinishWithSave:(BOOL)save
{
NSLog(@"Delegate Method fired");
if (save)
{
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
//The notification isn't firing becuase addPatientManagedObjectContext is null for some reason
[dnc addObserver:self selector:@selector(addControllerContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:addPatientManagedObjectContext];
NSError *error;
//if (![patient.managedObjectContext save:&error])
if (![addPatientManagedObjectContext save:&error])
{
NSLog(@"Before Error");
//Handle the error...
NSLog(@"Unresolved Error %@, %@",error, [error userInfo]);
exit(-1);//Fail
NSLog(@"After Error");
}
[dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:addPatientManagedObjectContext];
}
self.addPatientManagedObjectContext = nil;
[self.tableView reloadData];
[self dismissModalViewControllerAnimated:YES];
}
- (void)addControllerContextDidSave:(NSNotification*)saveNotification {
NSLog(@"Save Notification Fired");
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
[context mergeChangesFromContextDidSaveNotification:saveNotification];
}