关于CoreData的基本使用,我前面有两篇博客已经讲过,现在我们来看看CoreData中打稍微高级点的部分使用,NSFetchedResultsController类的使用。使用NSFetchedResultsController类的原因:因为它可以实时的检测我们Context的变化,我们可以根据这些变化,让我们打UI有所改变。使用NSFetchedResultsController的大致流程:这个类就是类似于数据库中的表,相当于TableView里打数据源,我们返回这fetchedResultsController,然后对他进行操作,它里面有很多有用的属性。
1⃣️:我们声明两个属性,managedObjectContext和fetchedResultsController
@property(nonatomic,strong)NSManagedObjectContext *managedObjectContext;
@property(nonatomic,strong)NSFetchedResultsController *fetchedResultsController;
2⃣️:我们创建一个抓取数据库的查询方法
-(NSFetchedResultsController *)fetchedResultsController_bad{
if(_fetchedResultsController != nil){
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
fetchRequest.sortDescriptors = sortDescriptors;
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
3⃣️:我们在ViewDidLoad里面执行获取fetchedResultsController
NSError *error;
if(![[self fetchedResultsController_bad] performFetch:&error]){
NSLog(@"Error ! %@",error);
abort();
}
4⃣️:那这样,TableView中就有两个代理需要实现了,TableView的代理主要是在UI显示的时候,根据数据库更新UI,而fetchedResultsController打代理主要是在Context做改变的时候可以检测并刷新UI。
下面是TableView的代理更新UI代码:(基本上就是用self.fetchedResultsController代替掉了datasource)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *object = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [object valueForKey:@"name"];
cell.detailTextLabel.text = [object valueForKey:@"age"];
return cell;
}
下面就是fetchedResultsController的代理实现:
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller{
[self.tableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
[self.tableView endUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
//最重要,最关键的修改代码处。
case NSFetchedResultsChangeUpdate:{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = [object valueForKey:@"name"];
cell.detailTextLabel.text = [object valueForKey:@"age"];
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
-(void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
那这样,关于NSFetchedResultsController这个类打简单介绍和使用就结束了。