IOS --- NSFetchResultsController

NSFetchedResultsController和UITableView集成起来处理数据具有强大的灵活性。首先得到的好处是不需要将数据记录进行分页,不然,按照传统的做法,需要先查询出总的记录,然后再从纪录里面过滤,这样会进行两次操作,对内存消耗很大,处理不好,程序甚至可能崩溃。使用NSFetchedResultsController类不仅简单,还具有更高的性能,这个类自动帮助你记录分页的事情,得到表对应的Core Data对象也非常简单。

更重要的是,你在其他界面更新或者删除记录时,NSFetchedResultsController可以帮助你同步更新UITableView,你的UITableView和数据库同步将变得非常简单。

以下是实现NSFetchedResultsController的委托方法

/*
 *NSFetchResultsController类是将一个获取请求和一个上下文作为其输入并在获取请求中的数据改变时调用该类的委托方法
 */

- (NSFetchedResultsController *)fetchedResultsController
{
    NSLog(@"fetchedResultsController");
    //检测是否已经创建了fetchedResultsController
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    /*
     *你需要一个获取请求和一个上下文以能够使用fetchedResultsController
     */

    //你可以将获取请求视作SQL SELECT语句
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //基于上下文中的Event实体创建一个实体
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    //设置该实体由fetchRequest使用
    [fetchRequest setEntity:entity];

    //设置fetchRequest的批大小为单词接收的合理记录数量
    [fetchRequest setFetchBatchSize:20];

    //创建一个NSSortDescriptor,使用NSSortDescriptor对fetchRequest的结果排序(基于“timeStamp”字段降序排序)
    //(你可以将NSSortDescriptor视为SQL ORDER BY字句)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    //需要一个获取请求和一个上下文以能够使用fetchedResultsController
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}    
//方法通知你“获取结果控制器”将更改内同
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    NSLog(@"controllerWillChangeContent");
    //通过调用表示图的beginUpdates方法告知表更新即将发生
    [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;

        default:
            return;
    }
}

- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

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

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

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
//通知你“获取结果控制器”完成了其更改
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    NSLog(@"controllerDidChangeContent");
    //通过调用endUpdates方法告知表视图更新结束
    [self.tableView endUpdates];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值