开发者

I think I lost my Managed Object Context

开发者 https://www.devze.com 2022-12-08 21:56 出处:网络
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 P

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.navigati开发者_JS百科onController 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];  
}


It looks like you create the context, and store it in self

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

But then you call the "add" method on the other controller:

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

(remember, you released 'addingContext' up above, 'addingContext' is not guaranteed to contain anything valid at this point)

Looks like you should be passing self.addPatientManagedObjectContext rather than addingContext in your insertNewObjectForEntityForName:@"Patient" line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号