ManagedObjectContext cancel rollback not fully deleting inserted data

470 Views Asked by At

I've seen a bunch of responses on similar threads but after following their recommendations am still having a bizarre issue and am banging my head against the wall.

I have a table of people and have the option for the user to add an entry for another user by filling out a form. That constructor is shown here:

-(id)initWithContext:(NSManagedObjectContext *)context {

    self = [super initWithNibName:@"FamilyMemberInfoViewController" bundle:[NSBundle mainBundle]];

    if (self) {

        self.managedObjectContext = context;

mainUser = [NSEntityDescription insertNewObjectForEntityForName:@"FamilyMember" inManagedObjectContext:context];
    Details *userDetails = [NSEntityDescription insertNewObjectForEntityForName:@"Details" inManagedObjectContext:context];
    mainUser.details = userDetails;
    userDetails.familyMember = mainUser;

...etc.

and then if the user clicks the back button I call:

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {

...

           [self.managedObjectContext rollback];
           [mainUser release];
           self.managedObjectContext = nil;    

...

And when it goes to the parent table view controller, it reloads the data with the below code and, as expected, the object is gone.

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (viewController == self) {
        NSFetchRequest *familyMemberRequest = [NSFetchRequest new];
        NSEntityDescription *familyDescription = [NSEntityDescription entityForName:@"FamilyMember" inManagedObjectContext:self.managedObjectContext];
        familyMemberRequest.entity = familyDescription;

        NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                  @"isMainUser == 0"];
        [familyMemberRequest setPredicate:predicate];

        NSError *error;
        self.familyMembers = [managedObjectContext executeFetchRequest:familyMemberRequest error:&error];

        NSLog(@"Retrieved family members! Count = %i", self.familyMembers.count);
        [familyMemberRequest release];
    }
}

HOWEVER - when I click away from the tab with that tableView and back to it, it runs the same code again and finds the cancelled object in the managed object context.

But - then when I re-run the app, it is nowhere to be found.

If I try creating another new person and backing out, it shows the correct number of entries initially (without any of the cancelled objects), and then when I click to another tab and back it shows only the most recent cancelled object in addition to the ones that should be there.

I've tried all manner of clearing the managed object context in the FamilyMemberInfoViewController: [context reset], [context rollback], [context deleteObject:], [context processPendingChanges] etc. I've tried experimentally loading the familyMembers array twice, and even when I click the back button it still finds the correct number of objects, but when I click a different tab and then back it shows the extra object (which then continues to show up if I go there and back).

Any ideas?

0

There are 0 best solutions below