Iphone 了解NSFetchedResultsController的工作原理

Iphone 了解NSFetchedResultsController的工作原理,iphone,ios,Iphone,Ios,首先,如果你有时间帮助我,非常感谢 我一直在学习nsfetchedresultscoontroller,我试图尽可能多地理解它,但是,关于它的一些事情让我非常困惑。这段代码来自我在网上找到的教程。基本上,我有一个最简单的抓取控制器,它是用像这样非常奇特的方法设置的 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { // The fetch controller is about t

首先,如果你有时间帮助我,非常感谢

我一直在学习
nsfetchedresultscoontroller
,我试图尽可能多地理解它,但是,关于它的一些事情让我非常困惑。这段代码来自我在网上找到的教程。基本上,我有一个最简单的抓取控制器,它是用像这样非常奇特的方法设置的

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
    [self.tableView beginUpdates];
}


- (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];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

 (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
        entityForName:@"FailedBankInfo" inManagedObjectContext:_context];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
        initWithKey:@"details.closeDate" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = 
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
            managedObjectContext:_context sectionNameKeyPath:nil 
            cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    [sort release];
    [fetchRequest release];
    [theFetchedResultsController release];

    return _fetchedResultsController;    

}
  • 我是否需要每次为不同的视图控制器创建一个新的抓取控制器?例如,如果我在一个列表视图中有一个企业列表。然后我点击其中的一个,进入该公司的员工列表,我必须使用不同的fetchedViewController,因为我需要重新指定实体键,对吗?请参见上面我的实现

  • 在我当前的实现中,它非常适合列出项目。假设我有一个名为VC的视图控制器。init i已完全实现
    NSFetchedResultsController
    。我有一个barbutton add项和一个名为
    AddButton pressed
    的方法,当按下该方法时,会添加一个视图(从下至上)。我用同样的方法
    Entity myEntity=[NSEntityDescription insertNNewObjectForEntityForName:@“Entity”inManagedObjectContext:context_]然后我告诉代码进入另一个视图但是,当动画向上移动新导航控制器时,新单元格已在动画中显示。从我的理解这两种方法
    
    -(void)configureCell:(UITableViewCell*)单元格atIndexPath:(NSIndexPath*)indexPath
    -(UITableViewCell*)tableView:(UITableView*)tableView
    与上面代码段中的前两个方法一起被调用。我怎样才能修好它?我不想在当前视图上显示任何内容,除非我通过转到另一个视图添加了新实体。另一个对象被调用到上下文中的第二个对象,必须调用update方法或其他方法

  • 这段代码的作用是什么?尤其是id

    (void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id NSFetchedResultsSectionInfo)sectionInfo atIndex:(nsInteger)ChangeType的sectionIndex:(NSFetchedResultsChangeType)类型{

    } 编辑:神圣的$#$,格式化这令人沮丧。上面的代码中应该有didChangeSection:(id(小于等号)NSFetchedResultsSectionInfo(大于等号))


  • 4我在哪里可以了解更多有关这方面的信息。i、 e单个内容更新方法的作用以及调用它们的时间等。NSFetchedResultcontroller是否也充当单个数组?这意味着,如果我想要一个包含多个部分的tableview,我需要更多的ViewController?再次感谢

    我赞同布拉德的评论,但我会给你一些建议:

  • 基本上是的,对于要查询的每个实体,都需要一个新的NSFetchedResultsController。但是,您根本不需要使用和NSFetchedResultsController,这是一个很好的类,可以让您像正在讨论的那样使用tableView做一些有趣的事情

  • 这里的问题是,您正在调用托管对象上下文中的
    [NSEntityDescription insertNNewObjectForEntityForName:@“Entity”
    从tableViewController调用,并且只要调用NsfeetedResultsController,就会收到一条消息,说明插入了一行,从而触发
    控制器:didChangeObject:atIndexPath:forChangeType:NewIndexXPath:
    方法中的NsfeetedResultsChangeInsert路径。NSFetchedResultsController是一个对象,它监视从CoreData获取的数据是否有任何更改,然后让您能够在更改发生时执行操作。要解决显示问题,只需将insertNewObjectForEntityName调用移动到下一个viewController中,当您关闭该viewController时,就会有一个新行

  • NSFetchedResultsController的didChangeSection:delegate方法会通知您是否已从数据集中添加或删除节。请看我对#4的回答,这一点应该很清楚

  • NSFetechedResultsController允许您使用sectionNameKeyPath:init调用的一部分对它获取的数据进行“分段”。通过在实体中提供数据点的名称,可以将数据分组到多个部分中。因此,回到#3,如果您碰巧在初始化NSFetchedResultsController时指定的“section”键路径中插入了一个新实体,则会调用didChangeSection:delegate


  • 我建议您阅读苹果的文档,可能还有他们的。不要忘记下载并使用示例代码,在大多数情况下,它比文档中的理论更容易理解。

    我建议将此问题细化为一个问题,并将其他问题作为单独的问题提问。现在这里有太多的问题要回答。
    switch(type) {
    
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }