0

Got a problem logically deleting an object from table view. Idea is to set a flag to an item like "deleted" to 1 if item shouldn't be shown. Loading data predicate shouldn't load such rows.

Code with predicate and creation of NSFetchedResultController:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Photo"];
                request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"section" ascending:YES],
                                            [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
                request.predicate = [NSPredicate predicateWithFormat:@"tag = %@ and deleted == %@", self.tag, @(0)];

                self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                                    managedObjectContext:self.tag.managedObjectContext
                                                                                      sectionNameKeyPath:nil
                                                                                               cacheName:nil];

Deletion happens on swipe. Here is action for the swipe:

- (IBAction)deletePhoto:(UISwipeGestureRecognizer *)sender {
    CGPoint location = [sender locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    if (indexPath) {
        Photo *photo = [self.fetchedResultsController objectAtIndexPath:indexPath];
        [self.tag.managedObjectContext performBlock:^{
            photo.deleted = @(1); //This must trigger data change and view must be redrawn. But it's not happending for some reason.
         }];
    }
}

The problem here is that row becomes deleted only after application restart. For some reason FetchedResultsController doesn't remove changed value from data and table view still shows the row.

I tried remove the item from table view explicitly but got exception for incorrect number of item in section 0 (that's why I assume that the row is still in fetch result). Explicit call to [self.tableView reloadData] or [self performFetch] don't reload the data and item still there.

I'm almost ran out of ideas what should I do to reload the data.

Thanks in advance.

4

1 回答 1

0

我不应该将数据库中的字段命名为“已删除”。重命名它,现在应用程序工作正常。

于 2013-05-23T17:38:49.340 回答