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

原帖地址:http://blog.csdn.net/jasonblog/article/details/8528691

上一篇讨论到添加球员信息后,球员列表没有及时得到修改。这是由于之前我们简单地使用了一个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立刻可以得到刷新。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值