Core Data has following 2 entities
- Item
- Order- which has an attribue called
isAdded
of type Boolean.
Item has a one-to-one relationship called order
with Order
. Order
has a one-to-many relationship called items
with Item
.
All items have an order. An order can have zero or more items.
I am trying to show all items in a UITableView
whose order.isAdded
is false
.
I am creating NSFetchedResultsController
as follows-
let fetchRequest: NSFetchRequest<Item> = Item.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "order.isAdded == NO")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Item.item?.name, ascending: true)]
return NSFetchedResultsController(
fetchRequest: fetchRequest,
managedObjectContext: container.viewContext,
sectionNameKeyPath: nil,
cacheName: nil
)
In a UITableViewController
, the NSFetchedResultsController
is initialized and its delegate is set to observe the changes.
If a new item is created whose order.isAdded
is false, then the delegate method is called with newIndexPath.
If an item is deleted, then the delegate method is called to inform that the indexPath was removed.
In a particular scenario, the delegate method is not called.
Consider an item whose order.isAdded
is false. This item is shown in the UITableView
. If the order.isAdded
is set to true
, I expect that the item must be removed from the UITableView. However, no delegate method is called.
Can anyone point out why this is happening and how to fix this issue?
Edit- This is a duplicate of Changing a managed object property doesn't trigger NSFetchedResultsController to update the table view