CoreData is saving model without the save: method being called

Posted by chris on Stack Overflow See other posts from Stack Overflow or by chris
Published on 2010-04-08T23:33:02Z Indexed on 2010/04/08 23:53 UTC
Read the original article Hit count: 233

Filed under:
|

I have a list of items with a plus button in the navigation bar that opens up a modal window containing a table of the models attributes, that displays a form when the table items are clicked (pretty standard form style). For some reason if I click the plus button to open the form to create a new model, then immediately click the done button, the person model is saved. The action linked to the done button does nothing but call on a delegate method notifying the personListViewController to close the window.

The apple docs do state that the model is not saved after calling the insertNewObjectForEntityName: ...

Simply creating a managed object does not cause it to be saved to a persistent store. The managed object context acts as a scratchpad.

I am at a lost to why this is happening, but every time I click the done button I have a new blank item in the original tableView.

I am using SDK v3.1.3

// PersonListViewController - open modal window
- (void)addPerson {
    // Load the new form
    PersonNewViewController *newController = [[PersonNewViewController alloc] init];
    newController.modalFormDelegate = self;

    [self presentModalViewController:newController animated:YES];
    [newController release];
}

// PersonFormViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    if ( person == nil ) {
        MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    self.person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" 
                                                    inManagedObjectContext:appDelegate.managedObjectContext];
    }

    ...

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                              target:self action:@selector(done)];
    self.navigationItem.rightBarButtonItem = doneButton;
    [doneButton release];
}

- (IBAction) done {
    [self.modalFormDelegate didCancel];
}

// delegate method in the original listViewController
- (void) didCancel {
    [self dismissModalViewControllerAnimated:YES];
}

© Stack Overflow or respective owner

Related posts about core-data

Related posts about iphone