2

This is very weird situtaion of using NSFetchedResultsController with core data in iOS development by Swift.

At first, FetchedResultsController contains only info of non-changed sections but no changed section info, althought that change causes controllerDidChangeContent method call tableView.reloadData method and in turn invoke numberOfSections method.

But subsequent manual invokes, e.g. in viewWillAppear, of the FetchedResultsController results in correct showing of the changed section info along with other non-changed section info.

In other words, fetched change of an attribute referred in sectionNameKeyPath via cloudkit in a background context was saved to viewContext which in turn saved it to persistent container. During the process, controllerDidChangeContent was invoked and tableView.reloadData was called. At that moment, in numberOfSections, no changed section info can be observed in the fetchedResultsController.sections, which only contains info of non-changed sections. I have set that attribute name as first sort descriptor in the fetch request associated with the fetchedResultsController.

I can't really find any clue of the illogical; any thought is welcome.

Ron Liu
  • 188
  • 11
  • It’s akmost impossible help without seeing the relevant code. – Magnas Nov 05 '18 at 04:46
  • Magnas Thanks for your attention. There are way too many blocks to post. However, after observing the controller(_:didChange:atSectionIndex:for), it is to my surprise that the NSFetchedResultsChangeType to the change (I just update the attribute value with fetched ckRecord) is "DELETE". Now it makes sense for the subsequent processing. But I'm still struggling to find why the change type becomes "delete" instead of "update". Any hint? – Ron Liu Nov 05 '18 at 05:06

1 Answers1

0

Issue found!!! Credit giving to Michael in answering the NSFetchedResultsController calls didChangeObject delete instead of update

Cause is my predicate using string interpolation for number value. After changing the use of number predicate, magic happens. Thanks Stackoverflow community!

Ron Liu
  • 188
  • 11