2009-10-12 80 views
0

所以我的工作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]; 
} 
+0

確定。我有這種工作。除非我回到我的主屏幕並再次返回,否則tableView不會重新加載,所以我想我意外地存儲到主MOC而不是詳細的。嗯 – monotreme 2009-10-12 22:44:57

回答

0

看起來你創建上下文,並將其存儲在self

NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init]; 
self.addPatientManagedObjectContext = addingContext; 
[addingContext release]; 

但隨後調用其他控制器上的 「添加」 方法:

patientAddViewController.patient = (Patient *)[NSEntityDescription 
    insertNewObjectForEntityForName:@"Patient" inManagedObjectContext:addingContext]; 

(記住,你發佈的「addingContext」了上面,「addingContext」不能保證包含任何有效的在這個POI nt)

看起來您應該在您的insertNewObjectForEntityForName:@"Patient"行中傳遞self.addPatientManagedObjectContext而不是addingContext

+0

感謝您的建議。試過這個。沒有工作。我認爲它主要是通過底部的通知方法來完成的。它只是不會觸發,我想因爲我已經混淆了addsContext,addPatientManagedObjectContext和主要的ManagedObjectContext。 – monotreme 2009-10-12 22:28:01

相關問題