【问题标题】:NSFetchedResultsController change tracking across relationshipNSFetchedResultsController 跨关系更改跟踪
【发布时间】:2025-11-30 07:30:01
【问题描述】:

我有一个模型,其中包含两个通过一对一关系链接的实体。我想在 TableView 中显示 entityB.myIntAttribute 等于给定值的所有 EntityA 对象。这在我使用谓词 @"entityB.myIntAttribute == aValue" 为 EntityA 创建 FetchRequest 时有效。

我的问题在于更改跟踪。我在与前一个 FetchRequest 关联的 NSFetchedResultsController 上设置了 NSFetchedResultsControllerDelegate 委托。这个委托之所以有效,是因为如果我设置了一个与其中一个结果的谓词不匹配的新 entityB,它会收到删除通知。但是,如果我直接更改 entityB 对象上 myIntAttribute 的值以使谓词结果发生更改,则不会通知我的委托。

NSFetchedResultsController 变更跟踪是否适用于跨越关系的谓词? FetchedResultController 如何在修改关系属性后重新计算谓词?

【问题讨论】:

  • 在下面看我的答案,评论不会让我添加足够的字符

标签: core-data nsfetchedresultscontroller relationship


【解决方案1】:

您是否在您的委托中实现了以下所有方法:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller

【讨论】: