NSFetchedResultsController in iOS 10 fires unexpected events on reordering

Number:rdar://27957917 Date Originated:2016-08-23
Status:Open Resolved:
Product:iOS SDK Product Version:Xcode Version 8.0 beta 6 (8S201h), iOS 10.0 (14A5339a)
Classification: Reproducible:Always
I have an NSFetchedResultsController that is sorted on an integer attribute. When that attribute is changed on two objects so that they change places in the order I would, in verions prior to iOS 10, get two events of the type NSFetchedResultsChangeMove - for example, one from index 0 to 1 and one from index 1 to 0. Now I get, instead, one NSFetchedResultsChangeMove from index 1 to 0 and one NSFetchedResultsChangeUpdate on index 0. 

SIDE NOTE: Updating a collection view accordingly then becomes difficult. If the move is handled with a "[UICollectionView moveItemAtIndexPath:toIndexPath:]" and the update with a "[UICollectionView reloadItemsAtIndexPaths:]", as is typical? (at least working in our app), I now get an error and unexpected behavior:

[error] error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  attempt to perform an insert and a move to the same index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}) with userInfo (null)

Steps to Reproduce:
1. Open the attached example app and run it. 
2. Press the button "Create things".
3. Press the button "Toggle order". 

Expected Results:
Running the above in Xcode 9.3, running in the simulator in Xcode 7.3.1, gives the following log output when clicking "Toggle order":

2016-08-23 01:25:03.858 CoreDataReorder[1976:163563] controllerWillChangeContent:
2016-08-23 01:25:03.859 CoreDataReorder[1976:163563] MOVE from 1 to 0
2016-08-23 01:25:03.859 CoreDataReorder[1976:163563] MOVE from 0 to 1
2016-08-23 01:25:03.859 CoreDataReorder[1976:163563] controllerDidChangeContent:

Actual Results:
Running the above in iOS 10.0 beta, running in the simulator in Xcode 8 beta 6, give sthe following log outputwhen clicking "Toggle order":

2016-08-23 01:22:18.762 CoreDataReorder[1809:155293] controllerWillChangeContent:
2016-08-23 01:22:18.762 CoreDataReorder[1809:155293] MOVE from 1 to 0
2016-08-23 01:22:18.762 CoreDataReorder[1809:155293] UPDATE at 0
2016-08-23 01:22:18.762 CoreDataReorder[1809:155293] controllerDidChangeContent:

Xcode Version 8.0 beta 6 (8S201h), iOS 10.0 (14A5339a)


iPhone Simulator


Test code: https://gist.github.com/skagedal/5fafe1daa35177536dabbe4415d6274

By simon.reimer at Aug. 23, 2016, 6:51 a.m. (reply...)

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!