NSFetchedResultsControllerDelegate methods are called twice

Originator:chrisdavidjones
Number:rdar://10207615 Date Originated:29-Sep-2011 11:16 AM
Status:Open Resolved:
Product:iPhone SDK Product Version:4.3
Classification:Other Bug Reproducible:Always
 
Summary:

When a NSManagedObjectContext is saved with both inserts and updates/deletes and an NSFetchedResultsController merges those changes, the NSFetchedResultsControllerDelegate methods are called twice, once for inserts and again for updates/deletes.

This causes a situation where - controllerDidChangeContent: is sent while the fetched results controller is in an intermediate state, having only processed a portion of the changes in the change notification.

I've tested this in both the iPhone Simulator 4.3.2 and on an iPhone running iOS 4.3.5, with identical results.


Steps to Reproduce:

Attached is the CoreDataBooks sample project, updated with a toolbar button that triggers the behavior. Console messages are logged when each delegate method is called.

The steps to reproduce are:

1. Query results using an NSFetchedResultsController
2. Insert a record and update or delete another record in a separate context.
3. Save the context
4. The NSFetchedResultsController will process the change notification and call the delegate methods twice, once for the insert and again for the update/delete.


Expected Results:

- controllerWillChangeContent:
process all inserts, updates, moves, and deletes
- controllerDidChangeContent:

The documentation for NSFetchedResultsControllerDelegate is not explicit that this is the intended behavior, but it does hint at it:

"Rather than responding to changes individually (as illustrated in “Typical Use”), you could just implement controllerDidChangeContent: **(which is sent to the delegate when all pending changes have been processed)** to reload the table view."

I expect the console log from the attached sample app to look like this:

2011-09-29 11:02:51.573 CoreDataBooks[10229:b603] --> content will change
2011-09-29 11:02:51.574 CoreDataBooks[10229:b603] did change object
2011-09-29 11:02:51.575 CoreDataBooks[10229:b603] insert object
2011-09-29 11:02:51.579 CoreDataBooks[10229:b603] did change object
2011-09-29 11:02:51.581 CoreDataBooks[10229:b603] delete object
2011-09-29 11:02:51.584 CoreDataBooks[10229:b603] did change object
2011-09-29 11:02:51.585 CoreDataBooks[10229:b603] update object
2011-09-29 11:02:51.586 CoreDataBooks[10229:b603] --> content did change
2011-09-29 11:02:51.587 CoreDataBooks[10229:b603] fetched objects count: 11


Actual Results:

– controllerWillChangeContent:
process all inserts
– controllerDidChangeContent:

process updates and deletes:
– controllerWillChangeContent:
process all updates, moves, and deletes
– controllerDidChangeContent:

The actual console log from the attached sample app looks like this:

2011-09-29 11:02:51.573 CoreDataBooks[10229:b603] --> content will change
2011-09-29 11:02:51.574 CoreDataBooks[10229:b603] did change object
2011-09-29 11:02:51.575 CoreDataBooks[10229:b603] insert object
2011-09-29 11:02:51.575 CoreDataBooks[10229:b603] --> content did change
2011-09-29 11:02:51.576 CoreDataBooks[10229:b603] fetched objects count: 12
2011-09-29 11:02:51.578 CoreDataBooks[10229:b603] --> content will change
2011-09-29 11:02:51.579 CoreDataBooks[10229:b603] did change object
2011-09-29 11:02:51.581 CoreDataBooks[10229:b603] delete object
2011-09-29 11:02:51.584 CoreDataBooks[10229:b603] did change object
2011-09-29 11:02:51.585 CoreDataBooks[10229:b603] update object
2011-09-29 11:02:51.586 CoreDataBooks[10229:b603] --> content did change
2011-09-29 11:02:51.587 CoreDataBooks[10229:b603] fetched objects count: 11


Notes:

This definitely looks like a bug to me, but if it's the intended behavior, then the documentation should be updated to clearly describe this behavior.


Attachment: http://yaychris.com/radar/10207615/CoreDataBooks.zip



07-Oct-2011 05:15 PM Apple Developer Bug Reporting Team :
Please test this issue with the iOS 5 GM seed (Build #: 9A334) and reply back with your results.

iOS GM seed 9A334
https://developer.apple.com/devcenter/ios/index.action#
Date Posted: October 4th, 2011



10-Oct-2011 10:39 AM Chris Jones:
Built against the iOS 5 SDK in Xcode 4.2 4D199 and tested on the Simulator running 9A334 and experienced the same issue described in this report.

Comments

I too am experiencing this issues against iOS 8.4. This has been open since 2011. Has this been resolved yet? What is the official status on this issue?


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!