0

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

  • after setting order.isAdded to true are you saving it on maincontext?? – Muhammad Afzal Jan 06 '20 at 05:34
  • No. I did not save the context. But even after saving, the issue is still observed –  Jan 06 '20 at 05:39
  • can you show the code for adding observer and when new order is added (isAdded is false) . – Vasucd Jan 06 '20 at 05:40
  • can you update code how you are updating isAdded to true?? are you using the following to check updates?? func controllerDidChangeContent(_ controller: NSFetchedResultsController) – Muhammad Afzal Jan 06 '20 at 05:46
  • I think the answers to the question you linked address both of your points - why it's happening and how to fix it. – pbasdf Jan 06 '20 at 10:18

0 Answers0