我想我丢失了托管对象上下文

时间:2009-10-12 22:03:08

标签: iphone core-data

所以我正在研究CoreDataBooks的克隆。 这有点不同。按下“+”按钮后,它会启动一个包含2个视图的navController。第一个(AddPatientVC)询问患者的姓名,然后将其推送到第二个视图控制器(AddPatientDetailVC),该控制器要求提供更详细的信息。这是第二个视图控制器,我已经设置了委托,而不是第一个,就像在CoreDataBooks中一样。

出于某种原因,当触发委托方法时,通知方法不会被触发,所以我不知何故忘记了我的MOC,无论是添加新患者的特定MOC。

我得到的具体错误是:'+ entityForName:无法找到实体名称'Patient'的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];  
}

1 个答案:

答案 0 :(得分:0)

看起来您创建了上下文,并将其存储在self

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

然后你在另一个控制器上调用“add”方法:

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

(请记住,你在上面发布了'addingContext','且无法保证'addsContext'包含任何有效的内容)

看起来您应该在self.addPatientManagedObjectContext行中传递addingContext而不是insertNewObjectForEntityForName:@"Patient"