开发者

Delete NSManageObject at the event tableView:commitEditingStyle:forRowAtIndexPath:

开发者 https://www.devze.com 2022-12-29 09:17 出处:网络
I got exception when I tried to delete one NSManageObject at the event of tableView:commitEditingStyle:forRowAtIndexPath:.Here is the part of my codes:

I got exception when I tried to delete one NSManageObject at the event of tableView:commitEditingStyle:forRowAtIndexPath:. Here is the part of my codes:

- (void)tableView:(..)tableView commitEditingStyle:(..)editingStyle 
  forRowAtIndexPath:(..)indexPath {
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    [managedObjectContext deleteObject:
      [fetchedResultController objectAtIndexPath:indexPath]];
     ...
  }
}

The exception was thrown at the line of deleteObject:(method of my local NSManagedObjectContext). This is the exception message:

uncaught exception 'NSObjectInaccessibleException', reason: 'The NSManagedObject with ID:0x3d07a30 <x-coredata://0D2CC9CB-042B-496D-B3FE-5F1ED64EAB97/paymentType/p2> has been invalidated.'

I tried to get entity object first and then to delete it. The entity looks OK but still the exception was at delete:

 NSManagedObject *enti开发者_StackOverflow中文版tyToDelete = 
    [fetchedResultsController objectAtIndexPath:indexPath];
 [mangedObjectContext deleteObject:entityToDelete]; // Exception again.

I am not sure if the entity object retrieved from the fetchedResultsController(NSFetchedResultsController type) cannot be deleted? If so, is there any other way to get the entity object for deleting?

I found that in the Apple's Core Data Tutorial for iPhone with events example, there is on NSArray to hold event entity objects. I am not sure if that's necessory to use NSArray to hold my local entity objects and then use it for deleting?


I think I found out the reason why I could not delete my entity object from my NSManagedObjectContext. It was the invalide NSManagedObjectContext object in my UITableViewController class. In short, I missed to retain the context object in property getter. The codes in my question is fine. When the fetched result controller and context objects are fine, the deletion works.

0

精彩评论

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

关注公众号