ios - NSFetchedResultsController:在后台线程中获取

标签 ios multithreading core-data uiviewcontroller nsfetchedresultscontroller

我有一个或多或少基本的 UITableViewController 和一个 NSFetchedResultsControllerUITableViewController 被推送到 navigationController 的 堆栈上。但是推送动画并不流畅,因为 NSFetchedResultsController 的获取是在主线程上执行的,因此会阻塞 UI。

我的问题是:如何在后台线程中执行 NSFetchedResultsController 的提取以保持动画流畅?

NSFetchedResultsController 和委托(delegate)方法如下所示:

- (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];
}

最佳答案

长话短说;没有充分的理由在主队列上使用上下文。

can use NSFetchedResultsController to fetch data in background

当然。 NSFetchedResultsController 可以与私有(private)队列上下文一起使用。事实上,这样做是非常快乐和高效的。 There is a bug这可以防止 NSFetchedResultsController 在使用私有(private)队列时使用它的缓存,但是缓存并没有像在 iOS 3.0 中那样赢得你的青睐。将 cacheName 设置为 nil 就可以了。

1. 使用 NSPrivateQueueConcurrencyType 创建上下文。最好不是您用于 IO 的那个。

2. 使用该上下文和缓存名称 nil 创建获取的结果 Controller 。

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. 您的所有委托(delegate)回调现在都将从上下文的队列中发生。如果您使用它们来更新 View ,请像上面看到的那样通过分派(dispatch)到主队列来执行此操作。

5. ...

6. 利润!

您可以阅读更多相关信息 here .

关于ios - NSFetchedResultsController:在后台线程中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803205/

相关文章:

c++ - cocos2d CCLayer.m UIAccelerometer 已弃用警告

ios - 插页式实现

ios - 为什么编译器认为这个变量未声明?

ios - Swift:当对象在一个区域内时运行代码

ios - Appium 与 MochaJS "Not JSON response"尝试运行 iOS 测试时

c++ - 两个线程访问的共享队列的临界区代码是什么?

c# - Parallel.ForEach 用例

Python并行编程模型

iphone - 导出 CoreData 的快捷方式是什么?

cocoa - 核心数据: multiple entities related to a single entity