关于Core Data的一些整理(五)

在Core Data中使用NSFetchedResultsController(以下简称VC)实现与TableView的交互,在实际中,使用VC有很多优点,其中最主要的是下面三点:

  1. Sections,你可以使用关键字将大量数据分隔成一段段的Section,在使用TableView时设置headerTitle时尤为好用
  2. Caching,在初始化VC时设置Caching名字即可使用,可以大量节约时间
  3. NSFetchedResultsControllerDelegate,监控数据的变动
 //首先是VC的初始化如下
//生成fetch请求
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Team"];
//添加排序方式
NSSortDescriptor *zoneSort = [NSSortDescriptor sortDescriptorWithKey:@"qualifyingZone" ascending:YES];
NSSortDescriptor *scoresort = [NSSortDescriptor sortDescriptorWithKey:@"wins" ascending:NO];
NSSortDescriptor *nameSort = [NSSortDescriptor sortDescriptorWithKey:@"teamName" ascending:YES];
fetchRequest.sortDescriptors = @[zoneSort, scoresort, nameSort];
//初始化NSFetchedResultsController,并以qualifyingZone来分为N个section,添加名为worldCup的缓存
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.coreDataStack.context sectionNameKeyPath:@"qualifyingZone" cacheName:@"worldCup"];
self.fetchedResultsController.delegate = self;
[self.fetchedResultsController performFetch:nil]; //下面是VC协议的实现,如第一个函数所见,VC与tableView有相对应的处理类型:Insert、Delete、Update、Move
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
TeamCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:cell withIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
default:
break;
}
}
//需要注意的一点是,要有下面对tableView的更新方法,否则会报错
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
} - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}

最新文章

  1. JMeter中的场景执行持续时间设置
  2. curl的POST与GET方法
  3. ytu 1938:首字母变大写(水题)
  4. Error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)"
  5. HDU1151Air Raid(二分图的最大匹配)
  6. java split函数 对空的处理
  7. springMVC3学习(五)--MultiActionController
  8. jsp获取服务端的访问信息
  9. 带你了解SDL
  10. c/c++ 网络编程 UDP 改变IP地址
  11. Java容器解析系列(4) ArrayList Vector Stack 详解
  12. CentOS 7 下sendEmail发邮件失败,提示invalid SSL_version specified at /usr/share/perl5/vendor_perl/IO/Socket/SSL.pm line 415.
  13. jQuery 学习笔记(2)(jQuery静态方法)
  14. flashsim配置2015最新版本
  15. linux 学习之路(学linux必看)
  16. Python Tkinter基础控件入门实例
  17. Day 24 继承
  18. springcloud-07-eureka HA的高可用配置
  19. VS2017 C++操作mysql数据库
  20. bzoj1005: [HNOI2008]明明的烦恼 prufer序列

热门文章

  1. hdu 5063 Operation the Sequence
  2. Integer Intervals(贪心)
  3. 【效率】FIND
  4. ubuntu server配置xmanager
  5. 图论(网络流):[HNOI 2013]切糕
  6. Subsets II ——LeetCode
  7. php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
  8. Swap[HDU2819]
  9. 性能比较工具runstats
  10. 如何看懂Code128条形码