所以我的工作CoreDataBooks的克隆。 有点不同。当按下“+”按钮时,它将启动一个包含2个视图的navController。第一个(AddPatientVC)询问病人的姓名,然后将其推送到第二个视图控制器(AddPatientDetailVC),该控制器询问更详细的信息。这是第二个视图控制器,我已经设置了代理,而不是像CoreDataBooks中的第一个。我觉得我失去了我的托管对象上下文
出于某种原因,当委托方法被触发,该通知方法不被解雇,所以我莫名其妙地失去了跟踪我的MOC的,无论是具体的MOC添加一个新的病人。
特定的错误我得到的是:“+ entityForName:未能找到NSManagedObjectModel为实体名称'病人”
这里是我的代码 - addPatient,委托方法和通知方法。任何关于简化的建议将不胜感激。感谢名单
-(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];
}
确定。我有这种工作。除非我回到我的主屏幕并再次返回,否则tableView不会重新加载,所以我想我意外地存储到主MOC而不是详细的。嗯 – monotreme 2009-10-12 22:44:57