The following code deletes a core data object if the user did not end up modifying the new entry (this happens on an insert operation). The root view controller created the entity and passed a reference to my view. It works fine if the user hits a "d开发者_开发知识库one" button on my view, but if instead they navigate backwards using the "back" button on the nav bar, the root view it returns to just hangs forever.
Am I doing something obviously wrong? I have considered waiting to create the entity until the user is finished with the view, but in the future this view will also handle editing existing entities, so my current method of passing an existing entity to this view is preferred.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// If the baby has no name, delete the Baby object that the root view
// already created. Otherwise save it.
NSManagedObjectContext *context = self.baby.managedObjectContext;
if ( [self.babyNameField.text length] == 0 )
[context deleteObject:baby];
// Save
NSError *error = nil;
if (![context save:&error]) {
// unresolved jmu - handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
This is a common approach and this code looks fine.
Have you set a breakpoint on it and walked through to make sure? You need to debug and track down where the code is hanging.
精彩评论