当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - NSFetchedResultsController - deleting rows - How to get access to managed object before row deletion?

问题描述:

I'm using a NSFetchedResultsController to display my core data and I also have a user generated images in a ubiquitous documents container, for each managed object the fetchedResultsController displays.

I've implemented the swipe left to delete functions on the TableView, but I'm having trouble accessing the managed object before the fetchedResultsController deletes my object. I need this access to also delete the user generated images in my iCloud container.

Here's the function I'm using:

func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {

switch type

{

case .Insert:

tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Automatic)

case .Delete:

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

default:

break

}

}

网友答案:

Why is the controller responsible for deleting the data? If other controllers delete managed objects, they have to implement image deletion too? Hardly a good solution.

I think it would make much more sense to intercept the deletion in the managed object subclass and make sure the image is deleted as well. You can use the managed objects's prepareForDeletion hook.

网友答案:

Are you able to intercept the table view row deletion command via this callback?

// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

As the header comment points out, you're not able to use this callback if you're using UITableViewRowActions.

If you're using row actions, then I believe that you should be able to intercept the deletion command in the handler:

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
分享给朋友:
您可能感兴趣的文章:
随机阅读: