关于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];
}

关于Core Data的一些整理(五)的更多相关文章

  1. 关于Core Data的一些整理(四)

    关于Core Data的一些整理(四) 调用Core Data文件中的Request模板进行条件匹配 //获取ObjectModel相当于获取Core Date文件 NSManagedObjectMo ...

  2. 关于Core Data的一些整理(三)

    关于Core Data的一些整理(三) 关于Core Data Stack的四种类与它们的关系如下: NSManagedObjectModel NSPersistentStore NSPersiste ...

  3. 关于Core Data的一些整理(二)

    关于Core Data的一些整理(二) 创建NSManagedObject的子类时,有一点是在这中间要强调的一点是,要不要勾选 Use scalar properties for primitive ...

  4. 关于Core Data的一些整理(一)

    关于Core Data的一些整理(一) 在Xcode7.2中只有Mast-Debug和Single View中可以勾选Use Core Data 如果勾选了Use Core Data,Xcode会自动 ...

  5. Core Data 学习简单整理01

    Core Data是苹果针对Mac和iOS平台开发的一个框架, 通过CoreData可以在本地生成数据库sqlite,提供了ORM的功能,将对象和数据模型相互转换 . 通过Core Data管理和操作 ...

  6. 《驾驭Core Data》 第一章 Core Data概述

    <驾驭Core Data>系列教程综合了<Core Data for iOS>,<Learning Core Data for iOS>,<Core Data ...

  7. 《驾驭Core Data》 第三章 数据建模

    本文由海水的味道编译整理,请勿转载,请勿用于商业用途.    当前版本号:0.1.2 第三章数据建模 Core Data栈配置好之后,接下来的工作就是设计对象图,在Core Data框架中,对象图被表 ...

  8. 《驾驭Core Data》 第二章 Core Data入门

    本文由海水的味道编译整理,请勿转载,请勿用于商业用途.    当前版本号:0.4.0 第二章 Core Data入门 本章将讲解Core Data框架中涉及的基本概念,以及一个简单的Core Data ...

  9. [Cocoa]深入浅出 Cocoa 之 Core Data(1)- 框架详解

    Core data 是 Cocoa 中处理数据,绑定数据的关键特性,其重要性不言而喻,但也比较复杂.Core Data 相关的类比较多,初学者往往不太容易弄懂.计划用三个教程来讲解这一部分: 框架详解 ...

随机推荐

  1. Multiple

    poj1465:http://poj.org/problem?id=1465 题意:给你一个数n(0~4999):以及m个不同十进制的数,问有这些十进制数组成的最小的n的倍数是多少.如果有则输出,没有 ...

  2. web.py simpletodo 例子

    一个很好的例子: 许多新手,特别是从 ASP/PHP/JSP 转过来的同学,经常问下面这几个问题: 所有东西都放在一个 code.py 中呀?我有好多东西该如何部署我的代码? 是不是 /index 对 ...

  3. sublime每次打开时都提示升级,怎么取消这个弹出框?

    答案其实很简单,设置如下: 进入Preferences -> Settings-User ,添加 "update_check": false 重启Sublime. 发现了什么 ...

  4. Lowest Common Ancestor in Binary Tree

    The problem: Given node P and node Q in a binary tree T. Find out the lowest common ancestor of the ...

  5. linux下的webserver BOA及CGIC库的使用指南(转帖)

    我把网页挂载到nfs 下面的文件中(需要新建一个文件www ),不过这样很方便! 安装过程 ====================================================== ...

  6. foxmail邮箱在代理环境下不能使用解决方法。

    由于工作原因,在域环境中但是还不是域用户,怎么设置代理也不能使用邮件客户端,幸亏老大给一软件,如下图 安装超级简单,而且软件很小,安装完后重启,基本不用设置就可以使用,前提是你的邮箱客户端要设置代理服 ...

  7. QTP自传之对象

    对象在手,测试我有 大家别误会,这里说的对象可不是值指男女朋友,而是对被测控件的识别.经过昨天的录制,大家一定很奇怪为什么我可以做到精确的回放操作,这都要归功于对象,下面就隆重的介绍我在对象识别方面的 ...

  8. HDOJ(HDU) 2309 ICPC Score Totalizer Software(求平均值)

    Problem Description The International Clown and Pierrot Competition (ICPC), is one of the most disti ...

  9. 转载:linux capability深入分析

    转至http://www.cnblogs.com/iamfy/archive/2012/09/20/2694977.html 一)概述:  1)从2.1版开始,Linux内核有了能力(capabili ...

  10. web前端面试试题总结---其他

    其他问题 原来公司工作流程是怎么样的,如何与其他人协作的?如何夸部门合作的? 你遇到过比较难的技术问题是?你是如何解决的? 设计模式 知道什么是singleton, factory, strategy ...