开发者

Deleting Core Data object before popping a view from the UINavigationController

开发者 https://www.devze.com 2022-12-19 23:06 出处:网络
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 referen

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.

0

精彩评论

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

关注公众号