NSInternalInconsistencyException on -[UICollectionView insertItemsAtIndexPaths:i

Originator:scott
Number:rdar://12954582 Date Originated:01/03/13
Status:Open Resolved:
Product:iPhone SDK Product Version:6.0
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
I am sporadically receiving an NSInternalInconsistencyException when attempting to insert new items to a UICollectionView. By sporadically, I mean 90% of the time, including when it works fine at first, but subsequent insertions (on the same build & run session) eventually receives an exception (e.g., after 4-5 insertions). I am aware that there is an existing issue with inserting into an empty section, so I have included a workaround (call reloadData instead of insertItemsAtIndexPaths:inSections: for the first insertion).

Steps to Reproduce:
I have two UICollectionViews displayed in a UIViewController via containers: ScheduleViewController and OfficeViewController. Double-tapping on an item in the Schedule collection presents a UIActionSheet with one prompt: Check In. Tapping Check In calls checkInPatientAtIndexPath:, in which the item is removed from the Schedule collection and passed to an NSNotification. OfficeViewController receives that NSNotification, adds the item to the collection data source, and then calls insertItemsAtIndexPaths:inSections:. I am aware that there is an issue with inserting into an empty section, so I have included a workaround (call reloadData instead of insertItemsAtIndexPaths:inSections: for the first insertion). After the first insertion, I get an NSInternalInconsistencyException almost every time. I noticed that, when an exception is about to occur, beforehand [self.collectionView indexPathsForVisibleItems] is empty, whereas when the insertion process is going to be successful, [self.collectionView indexPathsForVisibleItems] returns the expected results.

Expected Results:
UICollectionView should have completed insertion animation successfully.

Actual Results:
NSInternalInconsistencyException

Notes:

Console output (containing errors or exceptions)
2013-01-03 14:50:23.058 Precise Glance[37294:c07] *** Assertion failure in -[UICollectionViewData indexPathForItemAtGlobalIndex:], /SourceCache/UIKit_Sim/UIKit-2372/UICollectionViewData.m:442
2013-01-03 14:50:23.059 Precise Glance[37294:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for index path for global index 1 when there are only 1 items in the collection view'
*** First throw call stack:
(0x159b012 0x12a8e7e 0x159ae78 0xd3ef35 0x71ff20 0x705669 0x6fe5c6 0x700dc5 0x6fe31b 0x6fad4e 0x6fad86 0x2d3b 0xd69dc9 0x15f50c5 0x154fefa 0xc9e482 0xcb173b 0x5739 0x5c6a 0x5ce982 0x22e3b6 0x22e554 0x86f81f8 0x1067d8 0x23c5014 0x23b57d5 0x1541af5 0x1540f44 0x1540e1b 0x25fc7e3 0x25fc668 0x1f065c 0x235d 0x2285 0x1)
libc++abi.dylib: terminate called throwing an exception

Comments

Apple DTS acknowledgement

The issue that you have contacted us about turns out to be a known issue and is being tracked by radar numbers 12538212 and 12604781. I know that engineering is actively working on a fix for this issue but I do not have any insight into when those fixes might be released. So that you have a mechanism to track the progress of this problem I'd strongly suggest that you file a bug and attach your app and a set of steps to reproduce the problem so that engineering is aware that this issue is important to you. You can file a bug at:

http://developer.apple.com/bugreporter/

Once you have done that please send me the radar number so that I can track the progress of your problem. Once filed you will be able to track the progress of your bug by contacting devbugs@apple.com.

Now to the technical side of your issue. The problem turns out to be that when a UICollectionView has decoration views (in your case a section header) this can cause the UICollectionView to crash during updates. One workaround to this problem would be to remove the section header from your UI. In testing with your sample project I was able to make the UICollectionView behave properly by removing the section header. While less than ideal this might be a workaround you can use while waiting for the issue to be resolved by engineering.

I have noticed the issue even when a section header is not present. Just throwing that out there, in case someone else is running into the same problem.

Using reloadData for the first insertion fixes the problem as stated above by the OP.

By cyanide.nfs at Feb. 28, 2014, 11: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!