NSFetchedResultsController

本文介绍如何利用NSFetchedResultsController在Core Data中实现高效的数据加载,仅加载用户当前浏览的部分数据,提高应用性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSFetchedResultsController 是core data一个很好用的特性。 在理想的情况下,我们只载入用户正在浏览的那一部分的数据,幸运的是,苹果官方已经提供了一个这样做的方法,就是NSFetchedResultsController。
    
  1. - (NSFetchedResultsController *)fetchedResultsController {

  2. if (_fetchedResultsController != nil) {
  3. return _fetchedResultsController;
  4. }
  5. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  6. NSEntityDescription *entity = [NSEntityDescription
  7. entityForName:@"FailedBankInfo" inManagedObjectContext:managedObjectContext];
  8. [fetchRequest setEntity:entity];
  9. NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"details.closeDate" ascending:NO];//数据分类器 必须 升序
  10. [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
  11. [fetchRequest setFetchBatchSize:20];//取得的数据的缓冲值的最大值
  12. NSFetchedResultsController *theFetchedResultsController =
  13. [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
  14. managedObjectContext:managedObjectContext sectionNameKeyPath:nil
  15. cacheName:@"Root"];
  16. self.fetchedResultsController = theFetchedResultsController;
  17. _fetchedResultsController.delegate = self;

  18. return _fetchedResultsController;

  19. }
numberofrowsinsection 方法
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
id sectionInfo =
[[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {


    FailedBankInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];


    cell.textLabel.text = info.name;


    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@",


                                 info.city, info.state];


}//cellforatindexpath中yin用

代理方法 NSFetchedResultsControllerDelegate
- (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];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )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)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];
}










参考链接  大舌音
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值