iphone - 由于 NSManagedObjectContext 保存而更新 NSFetchedResultsController 时延迟 UITableView 更新

标签 iphone design-patterns core-data nsfetchedresultscontroller

我使用 NSFetchedResultsController 的谓词对 UITableView 进行排序。但是,当我在详细 View 中进行更改并保存它时,我更改的对象会立即放置在 UITableView 中的新位置。

我有类似于邮件中的消息 View 的向上/向下按钮。这种行为会破坏项目的排序,我想推迟此更改,直到用户出于 UX 原因退出 UITableView。我在 UITableViewController 中实现了 NSFetchedResultsControllerDelegate 方法。有没有一种简单/聪明的方法可以做到这一点?

编辑:下面是 UITableViewController 中实现的 NSFetchedResultsControllerDelegate 方法。

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
                    atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

最佳答案

问题不在于 NSFetchedResultsControllerDelegate 方法。仅当表可见时数据模型发生更改时,这些方法才会发挥作用。

您的问题是在 TableView 不在屏幕上且处于非事件状态时更改数据。 TableView 旨在自动显示给定的数据模型。当您从详细信息 View 返回 TableView 时,它会自动重新加载其数据,其中现在包含详细信息 View 中所做的更改。无论您如何向表提供数据,它都会这样做。

防止表在您需要之前显示更改的唯一方法是,在您希望显示更改之前不要将数据添加到数据模型。您必须将数据从详细 View 传递回非托管对象中的表,并在您希望它出现时在 TableView Controller 中创建托管对象。

但是,从 UI 设计的角度来看,我建议您重新考虑您的设计。如果您在重新加载表之前不更改表,那么您将如何向用户发出您将进行更改的信号? UI 会无缘无故突然更新吗?用户必须启动它吗?如果他们忘记了怎么办?

我认为用户希望详细信息 View 中所做的任何更改都能立即反射(reflect)在表格 View 中,因为这实际上是所有表格 View -详细 View 配对的工作方式。例如,如果您更改了地址簿的联系人详细信息中的联系人姓名,则当您返回联系人列表时,该姓名会立即反射(reflect)出来。

关于iphone - 由于 NSManagedObjectContext 保存而更新 NSFetchedResultsController 时延迟 UITableView 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3251872/

相关文章:

database - 可测试性的最佳数据库访问模式是什么?

objective-c - 类别对键@sum的键值编码不兼容

swift - 继承托管对象

ios - objective-c 使用 AutoResizingMask 将 UILabe 对齐到 UitableViewCell 内部

c# - 公共(public)属性(property)与静态只读

android - 连接器设计模式?

iphone - 如何像在 Entity Framework 中一样在核心数据中将属于一个类文件中的一个实体的方法分组?

ios - "Moving"自定义转换中跨 UIViewController 的 UIViews?

ios - iOS应用程序不适合iPhone 5屏幕

IOS 攻击者可以看到多少我的方法?