bur*_*rki 8 multithreading core-data uiviewcontroller nsfetchedresultscontroller ios
我有或多或少的基本UITableViewController使用NSFetchedResultsController.在UITableViewController被压入navigationController's堆栈.但推送动画并不流畅,因为提取NSFetchedResultsController是在主线程上执行的,因此会阻止UI.
我的问题是:如何NSFetchedResultsController在后台线程中执行获取以保持动画流畅?
在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];
}
Run Code Online (Sandbox Code Playgroud)
que*_*ish 20
TL; DR; 没有充分的理由在主队列上使用上下文.
可以使用NSFetchedResultsController在后台获取数据
绝对.NSFetchedResultsController可以与私有队列上下文一起使用.事实上,这样做非常快乐和高效.有一个错误,NSFetchedResultsController当它使用私有队列时阻止使用它的缓存,但缓存不会像在iOS 3.0中那样赢得你.设置一个cacheName零,你会没事的.
1.使用创建上下文NSPrivateQueueConcurrencyType.最好不要用于IO.
2. 使用该上下文创建获取的结果控制器,并将缓存名称设置为nil.
3.从performBlock:块中执行初始提取:
[[[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];
}];
}
}];
Run Code Online (Sandbox Code Playgroud)
4.现在,所有委托回调都将从上下文的队列中发生.如果您使用它们来更新视图,请通过调度到主队列来执行此操作,如上所示.
5. ...
6. 利润!
你可以在这里阅读更多相关信息.
Core Data 的一般规则是每个线程一个托管对象上下文,每个 MOC 一个线程。考虑到这一点,您需要在主线程上执行获取结果控制器的获取,因为这是将与 FRC 的托管对象交互的线程。(请参阅核心数据编程指南 - 与核心数据的并发)
如果您遇到动画性能问题,您可能应该寻找方法来确保在推送视图之前或之后执行提取。通常,您会在视图控制器的 中执行提取viewDidLoad:,并且导航控制器在提取完成之前不会推送视图。
| 归档时间: |
|
| 查看次数: |
8496 次 |
| 最近记录: |