NSFetchedResultsController:在后台线程中获取

我有一个或多或less的基本UITableViewController NSFetchedResultsControllerUITableViewController被推到navigationController's堆栈上。 但是推animation并不stream畅,因为NSFetchedResultsController的获取是在主线程上执行的,因此阻塞了UI。

我的问题是:如何在后台线程中执行NSFetchedResultsController的获取以保持animation的平滑?

NSFetchedResultsController和委托方法如下所示:

 - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"GPGrade" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; //Set predicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parent == %@", self.subject]; [fetchRequest setPredicate:predicate]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sortDescriptors = @[sortDescriptor]; [fetchRequest setSortDescriptors:sortDescriptors]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"SubjectMaster"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchedResultsController performFetch:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return _fetchedResultsController; } - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (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; } } - (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; break; case NSFetchedResultsChangeUpdate: //[self configureCell:(GPSubjectOverviewListCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 

核心数据的一般规则是每个线程一个pipe理对象上下文,每个MOC一个线程。 考虑到这一点,您需要在主线程上执行Fetched Results Controller的提取操作,因为这是将与FRC的Managed Objects交互的线程。 (请参阅核心数据编程指南 – 与核心数据并发 )

如果您对animation有性能问题,则应该考虑确保在推送视图之前或之后执行提取的方法。 通常情况下,您可以在视图控制器的viewDidLoad:执行提取,导航控制器在提取完成之前不会推送视图。

TL; DR; 在主队列上使用上下文没有什么好的理由。

可以使用NSFetchedResultsController在后台获取数据

绝对。 NSFetchedResultsController可以与私有队列上下文一起使用。 事实上,这样做是相当高兴和高效的。 有一个错误 ,它阻止NSFetchedResultsController在使用私有队列时使用它的caching,但caching不会像在iOS 3.0中那样赢得你。 设置一个cacheName为零,你会没事的。

1.使用NSPrivateQueueConcurrencyType创build一个上下文。 最好不是你用于IO的那个。

2.使用该上下文创build获取的结果控制器,并使用caching名称为nil。

3.performBlock: block中执行初始提取:

  [[[self fetchedResultsController] managedObjectContext] performBlock:^{ NSError *fetchError = nil; if (![self fetchedResultsController] performFetch:&error]){ /// handle the error. Don't just log it. } else { // Update the view from the main queue. [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [tableView reloadData]; }]; } }]; 

4.所有的委托callback现在都会从上下文的队列中发生。 如果您正在使用它们来更新视图,则可以通过像上面所看到的那样调度到主队列来执行此操作。

5.

6. 利润!

你可以在这里阅读更多。

核心数据的multithreading行为可以通过这个非常好的post。

希望能帮助到你..!!