3

我有一个由对象列表填充的NSFetchedResultsController-backed 。UICollectionViewController每个对象都有一个称为已删除的 BOOL 属性。我的 fetch 控制器使用以下谓词来过滤我的对象。

[NSPredicate predicateWithFormat:@"deleted == NO"];

我的问题是,当我通过将对象标记为deleted = @YES. 随后的didChangeObject:方法告诉我该对象更新且未删除 。并且该对象不会从集合视图中删除。如果我退出并重新加载我的应用程序,该对象不会显示在集合视图中,这是正确的行为。

有什么我做错了,还是这是预期的行为NSFetchedResultsController

此处要求的更新是代码:

获取控制器配置:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deleted == %@", @NO];
NSSortDescriptor *timestampSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
NSSortDescriptor *prioritySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"priority" ascending:NO];
NSManagedObjectContext *managedObjectContext = [NSManagedObjectContext MR_defaultContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:[GYNotification description] inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
[request setIncludesSubentities:YES];
[request setSortDescriptors:sortDescriptors];       
[request setPredicate:predicate];

NSFetchedResultsController *aFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                           managedObjectContext:managedObjectContext
                                             sectionNameKeyPath:sectionNameKeyPath
                                                      cacheName:nil];

然后获取控制器委托方法:

- (void)controller:(NSFetchedResultsController *)controller
   didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {
    NSMutableDictionary *change = [NSMutableDictionary new];
    switch(type) {
        case NSFetchedResultsChangeInsert:
            change[@(type)] = newIndexPath;
            break;
        case NSFetchedResultsChangeDelete:
            change[@(type)] = indexPath;
            break;
        case NSFetchedResultsChangeUpdate:
            change[@(type)] = indexPath;
            break;
        case NSFetchedResultsChangeMove:
            change[@(type)] = @[indexPath, newIndexPath];
            break;
    }
    [_objectChanges addObject:change];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    if ([_objectChanges count] > 0) {
        [self.collectionView performBatchUpdates:^{
            for (NSDictionary *change in _objectChanges) {
                [change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
                    NSFetchedResultsChangeType type = [key unsignedIntegerValue];
                    switch (type)
                    {
                        case NSFetchedResultsChangeInsert:
                            [self.collectionView insertItemsAtIndexPaths:@[obj]];
                            break;
                        case NSFetchedResultsChangeDelete:
                            [self.collectionView deleteItemsAtIndexPaths:@[obj]];
                            break;
                        case NSFetchedResultsChangeUpdate:
                            [self.collectionView reloadItemsAtIndexPaths:@[obj]];
                            break;
                        case NSFetchedResultsChangeMove:
                            [self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
                            break;
                    }
                }];
            }
        } completion:nil];
    }

    [_objectChanges removeAllObjects];
}
4

2 回答 2

5

这种行为听起来就像我对NSFetchedResultsController. 您实际上只是对您的一个对象进行更新。它被命名的事实deleted没有任何区别。事实上,deleted在我看来,调用它有点危险,因为NSManagedObject已经有了一个isDeleted方法。

听起来您想隐藏某些托管对象而不真正删除它们。我建议将该deleted属性重命名为类似shouldBeVisibleor markedForDeletion。这样,意图会更清晰一些,并且不会对您的对象真正处于什么状态感到困惑。

于 2013-10-08T14:29:25.237 回答
1

您需要像这样删除托管对象:

NSManagedObjectContext *context = ... ;
NSManagedObject *managedObjectToDelete = ... ;

[context deleteObject:managedObjectToDelete];

只有这样您才能NSFetchedResultsController检测到删除。

于 2013-10-08T14:14:54.097 回答