Core Data浅谈系列之七 : 使用NSFetchedResultsController

上一篇讨论到添加球员信息后,球员列表没有及时得到修改。这是由于之前我们简单地使用了一个NSMutableArray来管理球员列表,需要我们额外做一些变更通知。而在Core Data和UITableView之间,存在这一个名为 NSFetchedResultsController的类为我们提供更多方便。

从很大程度上来看,NSFetchedResultsController是为了响应Model层的变化而设计的。
在使用NSFetchedResultsController之前,我们需要为其设置一个NSFetchRequest,且这个fetchRequest必须得有一个sortDescriptor,而过滤条件predicate则是可选的。
接着,还需要一个操作环境,即NSManagedObjectContext。
通过设置keyPath,就是将要读取的entity的(间接)属性,来作为section分类key。
之后,我们为其设置可选的cache名称,以避免执行一些重复操作。
最后,可以设置delegate,用来接收响应变化的通知。 
[cpp]  view plain copy
  1. #pragma mark -   
  2. #pragma mark - NSFetchedResultsController  
  3.   
  4. - (NSFetchedResultsController *)fetchedResultsController  
  5. {  
  6.     if (nil != _fetchedResultsController) {  
  7.         return _fetchedResultsController;  
  8.     }  
  9.   
  10.     NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];  
  11.     NSEntityDescription *playerEntity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:self.cdViewController.managedObjectContext];  
  12.     [fetchRequest setEntity:playerEntity];  
  13.   
  14.     NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES];  
  15.     [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];  
  16.   
  17.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"team == %@", self.team];  
  18.     [fetchRequest setPredicate:predicate];  
  19.     [fetchRequest setFetchBatchSize:20];  
  20.   
  21.     _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.cdViewController.managedObjectContext sectionNameKeyPath:nil cacheName:@"Players"];  
  22.     _fetchedResultsController.delegate = self;  
  23.   
  24.     NSError *error = NULL;  
  25.     if (![_fetchedResultsController performFetch:&error]) {  
  26.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);  
  27.         abort();  
  28.     }  
  29.   
  30.     return _fetchedResultsController;  
  31. }  
这时候,我们将取消掉之前的playerArray,而是将self.fetchedResultsController作为UITableView的数据源:
[cpp]  view plain copy
  1. #pragma mark -  
  2. #pragma mark - UITableView DataSource  
  3.   
  4. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  5. {  
  6.     return [[self.fetchedResultsController sections] count];  
  7. }  
  8.   
  9. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  10. {  
  11.     return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];  
  12. }  
  13.   
  14. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  15. {  
  16.     staticNSString *cellIdentifier = @"TeamTableViewCellIdentifier";  
  17.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];  
  18.   
  19.     if (nil == cell) {  
  20.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];  
  21.     }  
  22.   
  23.     [self configureCell:cell atIndexPath:indexPath];  
  24.   
  25.     return cell;  
  26. }  
  27.   
  28. - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath  
  29. {  
  30.     Player *playerObject = [self.fetchedResultsController objectAtIndexPath:indexPath];  
  31.     cell.imageView.backgroundColor = [UIColor redColor];  
  32.     cell.textLabel.text = playerObject.name;  
  33.     cell.detailTextLabel.text = [playerObject.age stringValue];  
  34.     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  35. }  
为了在添加球员信息后,返回到上一个界面立即可以看到,我们需要重写对响应变化的代理函数。
这里有一份 经典用法代码片段,不过Demo里采取的是简单有效的方法,因为不需要动画效果(并且适用于大批量数据的更新): 
[cpp]  view plain copy
  1. #pragma mark -  
  2. #pragma mark - NSFetchedResultsController Delegate  
  3.   
  4. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller  
  5. {  
  6.     [self.playerTable reloadData];  
  7. }  
做完以上工作,在添加完球员信息后,UITableView立刻可以得到刷新。



Brief Talk About Core Data Series, Part 7 : Using NSFetchedResultsController

Jason Lee @ Hangzhou
CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分类,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值