gpt4 book ai didi

cocoa-touch - 核心数据 : updating a managed object doesn't call the NSFetchedResultsControllerDelegate

转载 作者:行者123 更新时间:2023-12-03 17:59:54 24 4
gpt4 key购买 nike

当我以编程方式更改一些现有的托管对象属性时,我的 TableView 没有更新,我遇到了问题。我希望我的 NSFetchedResultsControllerDelegate在这种情况下被调用,但事实并非如此。

苹果文档说:

An instance of NSFetchedResultsController uses methods in this protocol to notify its delegate that the controller’s fetch results have been changed due to an add, remove, move, or update operations.



更详细地说,我的托管对象是一个名为 status 的 int 属性。从 enum 中获取值.我的表格 View 只显示那些具有 status 的对象等于 upToDate (枚举值之一)。为此,我的 FetchedResultController 有一个谓词来测试该状态:
predicate = [NSPredicate predicateWithFormat:@"doc.status == %d", upToDate];

它工作正常,但是当我将过期对象更改回状态 upToDate 时不会反射(reflect)更新.我的代码是根据 Apple 的模板修改的。

这是我尝试过的:
myObject.status = [NSNumber numberWithInt:upToDate];
[managedObjectContext save:nil];
[managedObjectContext processPendingChanges];
[myTableView reloadData];

最后,我设法通过调用以下命令来更新表格 View :
[fetchedResultsController performFetch:&error];
[myTableView reloadData];

但即便如此,我的 NSFetchedResultsControllerDelegate 也没有。方法被调用,与 Apple 的文档相矛盾。此外,不会发生流畅的动画。

我期待 controllerWillChangeContent: , controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: , 和 controllerDidChangeContent:被称为。

我想我错过了一些东西,可能是一些明显的东西,但我看不出是什么。是否 performFetch:每次某些托管对象可能发生变化时都需要调用吗? NSFetchedResultsControllerD什么时候真的调用它的代表吗?

谢谢。

最佳答案

如果无论更改的类型如何,都不会调用 FRC 委托(delegate)方法,那么最可能的解释是委托(delegate)未正确配置。这可能是由以下原因引起的:

  • 委托(delegate)简单未设置
  • 委托(delegate)类没有声明 NSFetchedResultsControllerDelegate协议(protocol)。
  • 您无需合并即可在另一个上下文中进行更改。

  • 我建议捕获保存操作的错误返回,以确保保存没有发生任何事情。

    无论是否已保存,都应通知 FRC 其上下文的任何更改。然后 FRC 应通知其代表。

    关于cocoa-touch - 核心数据 : updating a managed object doesn't call the NSFetchedResultsControllerDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3607434/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com