核心数据:为什么必须调用重新加载数据才能使我的应用程序工作? [英] Core Data: Why reload data must be called to make my app work?

查看:77
本文介绍了核心数据:为什么必须调用重新加载数据才能使我的应用程序工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花整个晚上调试一个简单的应用程序。该应用程序从Web检索一个图像(是的,以使我的生活更轻松),并在表视图中显示它。我这样做是为了学习核心数据。在我修正之前,错误讯息如下:

I spend the whole night debugging a simple app. The app retrieves one image (yes one..intend to make my life easier) from web, and displays it in table view. I do that as a practice to learn Core Data. Before I fix it, the error message shows below:


2012-09-30 06:16:12.854缩图[34862:707] CoreData :错误:严重
应用程序错误。在调用
-controllerDidChangeContent:的过程中,
NSFetchedResultsController的委托捕获了一个异常。无效的更新:版面号无效。
更新(1)之后的表视图中包含的节的数量必须等于
中包含的节的数量更新前的表视图(0),加上或减去
插入或删除的节(插入0个,删除0个)。 with userInfo
(null)

2012-09-30 06:16:12.854 Thumbnail[34862:707] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)

基本上说, FRC委托方法出了问题 。一方面,段号从0变为1.另一方面,0插入,0删除。那么如何节号可以增加?因此错误。

Basically it's saying that something goes wrong with FRC delegate methods. At one hand, section number changes from 0 to 1. At the other hand, "0 inserted, 0 deleted". So how the section number can increase? That should not happen.. Hence the error.

我通过添加 [self.tableView reloadData] 到我的FRC设置方法。我从这个帖子获得灵感,但我不太明白。答案似乎太复杂和项目具体。有人可以解释为什么添加 reloadData 可以修复错误?答案可能是一个容易的,我希望如此。

I fix the bug by simply adding [self.tableView reloadData] to my FRC setup method. I got the inspiration from this post, yet I don't quite understand it. The answer seems like too complicated and project specific. Could someone explain why adding reloadData can fix the bug? The answer might be an easy one, I hope so.

我的应用程式的主要元件,如果这是重要的:

Key components of my app, if that matters:


  • 使用 UIManagedDocument 建立核心数据栈

  • 创建帮助方法从Flickr API下载图片

  • 在NSManagedObject子类文件中,尝试从持久存储获取图像。如果还没有,请将它插入MOC。

  • Use UIManagedDocument to establish the core data stack
  • Create a helper method to download images from Flickr API
  • In NSManagedObject subclass file, try fetch image from persistent store. If it's not there yet, insert it into MOC.

- (void)setupFetchedResultsController
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"BigImage" inManagedObjectContext:self.document.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *imageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"image" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObject: imageDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setFetchBatchSize:20];

    // Create fetch results controller
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.document.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];

    self.fetchedResultsController.delegate = self;

    NSError *error;
    if (![self.fetchedResultsController performFetch:&error])
    {
        NSLog(@"Error in performFetch: %@, %@", error, [error userInfo]);
    }

    // Critical!! I add this line to fix the bug!
        [self.tableView reloadData];
    }


推荐答案

抓取的结果控制器仅跟踪第一次抓取后对托管对象内容的更改。然后,使用委托方法 didChangeSection didChangeObject 等将这些更改传播到表视图。

A fetched results controller tracks only changes to the managed object contents after the first fetch. These changes are then propagated to the table view using the delegate methods didChangeSection, didChangeObject etc.

但是没有自动机制,初始提取的结果被发送到表视图。这就是为什么你必须在 performFetch 后调用 reloadData 的原因。

But there is no automatic mechanism that the result of the initial fetch is sent to the table view. That is the reason why you have to call reloadData after performFetch.

但是,有一种情况,这似乎自动工作。 UITableViewController 中调用 reloadData viewWillAppear 第一次)。因此,如果您在 viewDidLoad 中设置FRC,则中将调用 reloadData viewWillAppear ,您不必手动调用它。

However, there is a situation where this seems to work automatically. UITableViewController calls reloadData in viewWillAppear (if the table is loaded the first time). Therefore, if you set up the FRC for example in viewDidLoad, reloadData will be called in viewWillAppear, and you don't have to call it manually.

这篇关于核心数据:为什么必须调用重新加载数据才能使我的应用程序工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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