NSFetchedResultsController在UIManagedDocuments获取新数据后不更新TableView [英] NSFetchedResultsController does not update TableView after UIManagedDocuments gets new data

查看:180
本文介绍了NSFetchedResultsController在UIManagedDocuments获取新数据后不更新TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSFetchedResultsController 结合在后台线程中更新的 UIManagedDocument

I am using a NSFetchedResultsController in combination with a UIManagedDocument which gets updated in an background thread.

我已经完全按照本教程中的描述设置了NSFetchedResultsController:如何使用NSFetchedResultsController

I have set up the NSFetchedResultsController exactly as described in this tutorial: How To Use NSFetchedResultsController

我设置了委托 _fetchedResultsController.delegate = self 和我的视图控制器的协议 NSFetchedResultsControllerDelegate

I have set the delegate _fetchedResultsController.delegate = self and the protocol for my view controller to NSFetchedResultsControllerDelegate.

当它启动后加载数据。但是,每当处理和保存后台线程中的数据时, NSFetchedResultsController 不会更新TableView 。特别是,NSFetchedResultsController的委托方法 -controllerWillChangeContent:controller 等。从不被调用。

My code works fine when it loads the data after launch. However, the NSFetchedResultsController does not update the TableView whenever it has processed and saved the data in the background thread. In particular, the NSFetchedResultsController's delegate methods -controllerWillChangeContent:controlleretc. are never being called.

我已经仔细检查了SQLite数据库是否正确包含数据。
这是我在视图控制器中处理和保存数据的方式:

I have double-checked that the SQLite database contains the data correctly. This is how I process and save the data in the view controller:

[backgroundContext performBlock:^{
        [company parseAttributesFrom:xmlStr inManagedObjectContext:backgroundContext]; //self.managedDocument.managedObjectContext

        NSError *error = nil;
        [backgroundContext save:&error];
         if (error) NSLog(@"error: %@",error.localizedDescription);
        [self.managedDocument.managedObjectContext performBlock:^{
            NSError *error = nil;
            [self.managedDocument.managedObjectContext save:&error];
            if (error) NSLog(@"error: %@",error.localizedDescription);
        }];
        [self.managedDocument saveToURL:self.managedDocument.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
        [self.managedDocument updateChangeCount:UIDocumentChangeDone];
    }];

如何获取NSFetchedResultsController在底层数据更改时自动更新TableView?

How can I get the NSFetchedResultsController to update the TableView automatically when the underlying data changes?

感谢您的帮助!

推荐答案

是在managedObjectContext。您在后台进行更改, NSFetchedResultsController 从主要提取。因此,您需要通过添加上下文中的更改的观察者来合并对上下文的更改。

I think that reason is in managedObjectContext. You make changes in background, and NSFetchedResultsController fetches from the main one. So you need to merge changes to that one context by adding an observer of changes in context

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextChanged:) name:NSManagedObjectContextDidSaveNotification object:nil];

这里是一个伟大的教程由Marcus Zarra-核心数据的大师)
希望帮助。
http://www.cimgf.com/2011/08/22/importing-and-displaying-large-data-sets-in-core-data/

Here is a great tutorial by Marcus Zarra - The Guru of Core Data) Hope that helps. http://www.cimgf.com/2011/08/22/importing-and-displaying-large-data-sets-in-core-data/

这篇关于NSFetchedResultsController在UIManagedDocuments获取新数据后不更新TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆