应用场合:配合Core Data和Table View使用,Core Data存储数据,Table View显示数据

1.在需要显示数据的视图控制器(包含有table view)中添加NSFetchedResultsController实例变量

@implementation LocationsViewController
{

NSFetchedResultsController *_fetchedResultsController;

}

2.惰性实例化NSFetchedResultsController实例变量

-(NSFetchedResultsController *)fetchedResultsController

{

if(_fetchedResultsController == nil)

{

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];  //创建一个请求

    //实体描述

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:self.managedObjectContext];

[fetchRequest setEntity:entity];

//设置排序 

NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"category" ascending:YES];

NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];

[fetchRequest setSortDescriptors:@[sortDescriptor1,sortDescriptor2]];

    //每次获取20个数据

[fetchRequest setFetchBatchSize:20];

  //按“category”关键字分组,cacheName参数的名字用于唯一标记获取的数据结果,fetchedResultsController会将这些数据缓存起来

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"category" cacheName:@"Locations"];

_fetchedResultsController.delegate = self;

}

return _fetchedResultsController;

}

3.让视图控制器实现NSFetchedResultsController协议

@interface LocationsViewController () <NSFetchedResultsControllerDelegate>

@end

4.调用方法开始获取Core Data存储的数据

-  (void)viewDidLoad {

  [super viewDidLoad];

  [self performFetch];

}

-(void)performFetch

{

NSError *error;

if(![self.fetchedResultsController performFetch:&error])

{

//添加处理错误情况的代码

return;

}

}

5.添加以下代码

- (void)dealloc {

  _fetchedResultsController.delegate = nil;

}

6.实现table view数据源返回行数的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];

return [sectionInfo numberOfObjects];

}

7.获取Core Data存储的某一个数据(实体)时用以下语句,之后便可对该对象进行删除,修改等操作,然后再同步到Core Data数据(参考Core Data博文)

Location *location = [self.fetchedResultsController objectAtIndexPath:indexPath];

8.实现NSFetchedResultsController协议方法

#pragma mark - NSFetchedResultsControllerDelegate

-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller

{

NSLog(@"*** controllerWillChangeContent");

[self.tableView beginUpdates];

}

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(nonnull id)anObject atIndexPath:(nullable NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(nullable NSIndexPath *)newIndexPath

{

switch (type) {

case NSFetchedResultsChangeInsert:

NSLog(@"*** NSFetchedResultsChangeInsert (object)");

[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

break;

case NSFetchedResultsChangeDelete:

NSLog(@"*** NSFetchedResultsChangeDelete (object)");

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

break;

case NSFetchedResultsChangeUpdate:

NSLog(@"*** NSFetchedResultsChangeUpdate (object)");

//添加调用设置table view cell内容的方法,该方法需要另行新建

break;

case NSFetchedResultsChangeMove:

NSLog(@"*** NSFetchedResultsChangeMove (object)");

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

break;

}

}

-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(nonnull id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

{

switch (type) {

case NSFetchedResultsChangeInsert:

NSLog(@"*** NSFetchedResultsChangeInsert (section)");

[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

break;

case NSFetchedResultsChangeDelete:

NSLog(@"*** NSFetchedResultsChangeDelete (section)");

[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

break;

case NSFetchedResultsChangeMove:    break;

case NSFetchedResultsChangeUpdate:  break;

}

}

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller

{

NSLog(@"*** controllerDidChangeContent");

[self.tableView endUpdates];

}

9.实现以下方法,当滑动删除cell时,同时同步到Core Data数据库

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{

if(editingStyle == UITableViewCellEditingStyleDelete)

{

Location *location = [self.fetchedResultsController objectAtIndexPath:indexPath];

[self.managedObjectContext deleteObject:location];

NSError *error;

if(![self.managedObjectContext save:&error])

{

//添加处理错误情况的代码

return;

}

}

}

10.实现table view分组相关的协议方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return [[self.fetchedResultsController sections] count];

}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];

return [sectionInfo name];

}

11.可按以下格式设置cell的显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Location"];

[self configureCell:cell atIndexPath:indexPath];  //configureCell:atIndexPath:方法用于详细设置如何显示cell的内容,需另行编写

return cell;

}

转载于:https://www.cnblogs.com/guitarandcode/p/5572366.html

iOS开发学习之NSFetchedResultsController相关推荐

  1. ios开发学习-手势交互(Gesture)效果源码分享

    qianqianlianmeng ios开发学习-手势交互(Gesture)效果源码分享 All Around Pull View 介绍:实现视图四个方向(上下左右)都能够拖动更新(pull to r ...

  2. ios开发学习笔记--Core Motion

    iOS开发学习笔记之CoreMotion-运动传感器 官网文档:CoreMotion Framework Reference 一.     简介 现在的苹果手机都基本有运动传感器,能够过获取到设备的加 ...

  3. IOS开发学习笔记-----UILabel 详解

    IOS开发学习笔记-----UILabel 详解 01 //创建uilabel 02 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMa ...

  4. iOS开发学习48 OC的lambda block

    iOS开发学习48 lambda表达式 一.block 简介 二.block使用 1. block的写法大概就是这样: 2. 带参数的话可以这样写: 3. 如果不写入参,可以写: 4. 当然返回也可以 ...

  5. IOS开发学习笔记(一)

    概述: iOS是苹果开发的手持设备操作系统(iPhone,iPad,iPod touch,iPad mini). 基于UNIX,层次架构:核心操作系统层(Core OS)-> 核心服务层(Cor ...

  6. [IOS] iOS开发学习的站点

    iOS开发学习的好站点,纪录在此. http://www.cocoachina.com/ 适合iOS学习入门 开发者中心的内容适合开发时参考 http://code4app.com/ 大量iOS源码 ...

  7. IOS开发学习周报(二)

    IOS开发学习周报(二) 简介 课程名称 IOS开发实训 任课老师 郑贵锋老师&字节跳动工程师 学号 16340015 专业(方向) 软件工程(计应) 姓名 陈彬彬 Email 9441312 ...

  8. IOS开发学习周报(一)

    IOS开发学习周报(一) 学习记录 概括 熟悉在Mac OS下的开发操作,熟悉系统操作.了解相关快捷键以及触摸板快捷操作以便提高后续开发效率. 学习Objective-C相关语法,实操完成螺旋矩阵的算 ...

  9. IOS开发学习 IDE环境搭建教程

    IOS开发学习 IDE环境搭建教程是本文要介绍的内容,在坛子里逛了一圈,发现一篇好的文章,与友们分享一下,要有耐心的看整个过程,不多说,直接进入话题. 安装条件: 硬件:一台拥有支持虚拟技术的64位双 ...

  10. 【IOS 开发学习总结-OC-60】ipad应用开发的一些知识

    [IOS 开发学习总结-OC-60]ipad应用开发的一些知识 ipad与iPhone上 管理有层次的工作流的不同 iPhone上:通过NavigationController,用户可以从上一层界面A ...

最新文章

  1. 几种开源工作流引擎的简单比较(转)
  2. 区块链相关论文研读6: FASTKITTEN,关于比特币的实用智能合约
  3. cocos2dx 3.3 异步加载纹理
  4. 关于git pull机制和游戏开发热更新思考
  5. Redhat6.5安装vnc服务远程桌面
  6. 我的CSDN博客之旅
  7. 25 个超棒的 HTML5 JavaScript 游戏引擎开发库
  8. 可视化程序设计基础(team)——采访上届大佬
  9. 爱创课堂每日一题第四天8/28日XML和JSON的区别?
  10. JAVA程序性能优化研究 - 循环内对象性能优化内容
  11. 高等数学(第七版)同济大学 习题4-4(后14题) 个人解答
  12. 用python实现单词本功能
  13. 什么是请求报文和响应报文?
  14. 现今最强引擎对比!虚幻3 vs CE2 vs 寒霜2.0
  15. Socket 多人聊天室的实现 (含前后端源码讲解)(一)
  16. Cortex-M3 Bit-Banding
  17. 码支付系统 无授权—个人免签约支付系统二维码收款即时到账源码 –
  18. javaweb网上宠物商城管理系统分前后台(源码+数据库+开题报告+ppt+文档)
  19. 数据结构期末复习线性表(2)
  20. Educational Codeforces Round 128 (Rated for Div. 2) E. Moving Chips(玄学)

热门文章

  1. 【渝粤教育】国家开放大学2018年秋季 2245T社会福利与保障 参考试题
  2. 次小生成树的某些结论及其算法O(V^2)
  3. 将自己电脑的SSH key添加到GitHub上
  4. 网络编程(三)--通信循环、链接循环、粘包问题
  5. Codeforces Round #273 (Div. 2) D. Red-Green Towers DP
  6. Unity编辑器扩展之RequireComponent等详解
  7. [转] 各种数据类型转换
  8. h5前期js知识点10月16日总结
  9. kotlin-2(IdeaIU-2018.2汉化破解)
  10. Selenium 高阶应用之WebDriverWait 和 expected_conditions