IGListKit: Wrong index in sizeForItem
New issue checklist
- I have reviewed the
READMEand documentation - I have searched existing issues and this is not a duplicate
General information
IGListKitversion: 3.1.1- iOS version(s): iOS 10 & iOS 11
- Carthage version: 0.25
- Xcode version: Xcode 8 & Xcode 9
- Devices/Simulators affected: All devices and simulators
- Reproducible in the demo project? No (modified demo attached below).
- Related issues: NONE
Description
We use modified NestedAdapterViewController example. In subclass of ListSectionController we receive crash on sizeForItem(at: where we receive index out of bounds. We also receive crash on cellForItem(at with index out of bounds. It happens performUpdates from adapter, not reloadData.
I’m not sure if we use ListSectionController or adapter incorrectly or confirm it’s a bug in IGListKit. I would be grateful for help.
Link to modified example: https://github.com/rafalszastok/IGListKit/tree/crash-on-embedded-cells Step to reproduce: After list of examples appears, select “Out custom example” at the bottom. It should crash automatically after few seconds.
Classes added to project:
- RailsViewController.swift
- RailSectionController.swift
- TileSectionController.swift
- ViewModels.swift
Modified:
- DemosViewController.swift
Edit
I did some changes and now example does the same as EmbeddedSectionController and this time I receive different error. In this case batch update doesn’t finish before the next one starts. This cause the number of times isn’t updated yet.
2017-10-04 10:11:37.168914+0200 IGListKitExamples[15977:1224786] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (4) must be equal to the number of sections contained in the collection view before the update (8), plus or minus the number of sections inserted or deleted (2 inserted, 1 deleted).'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ea181cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010e37af41 objc_exception_throw + 48
2 CoreFoundation 0x000000010ea1d362 +[NSException raise:format:arguments:] + 98
3 Foundation 0x000000010de1f089 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 UIKit 0x0000000110ce597f -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 15755
5 UIKit 0x0000000110cee5ad -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 71
6 UIKit 0x0000000110cee8f2 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 435
7 UIKit 0x0000000110cee71c -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 91
8 UIKit 0x0000000110cee69e -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 74
9 UIKit 0x0000000110cee5f3 -[UICollectionView performBatchUpdates:completion:] + 53
10 IGListKit 0x000000010db41267 __62-[IGListAdapterUpdater performBatchUpdatesWithCollectionView:]_block_invoke.118 + 567
11 IGListKit 0x000000010db3f9c6 -[IGListAdapterUpdater performBatchUpdatesWithCollectionView:] + 3958
12 IGListKit 0x000000010db43aef __54-[IGListAdapterUpdater queueUpdateWithCollectionView:]_block_invoke + 463
13 libdispatch.dylib 0x00000001126c93f7 _dispatch_call_block_and_release + 12
14 libdispatch.dylib 0x00000001126ca43c _dispatch_client_callout + 8
15 libdispatch.dylib 0x00000001126d56f0 _dispatch_main_queue_callback_4CF + 628
16 CoreFoundation 0x000000010e9daef9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
17 CoreFoundation 0x000000010e99f662 __CFRunLoopRun + 2402
18 CoreFoundation 0x000000010e99ea89 CFRunLoopRunSpecific + 409
19 GraphicsServices 0x0000000113dee9c6 GSEventRunModal + 62
20 UIKit 0x0000000110214d30 UIApplicationMain + 159
21 IGListKitExamples 0x000000010d6f5da7 main + 55
22 libdyld.dylib 0x0000000112746d81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Branch: https://github.com/rafalszastok/IGListKit/tree/crash-on-embedded-cells-2
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 6
- Comments: 18 (8 by maintainers)
Ok, so there is something wrong here. The cached data inside
UICollectionViewhas gotten out of sync w/ what thecollectionView.dataSourcereports. Take a look at this debugger output:I can confirm this happens on both iOS 11 and 10.3.
If you update this line to be:
Then the crash goes away b/c we force the collection view to update its data source.
This seems like a pretty big deal tbh. I’m going to put up an experimental fix to see how often this happens to us. We used nested adapters a lot within Instagram, and almost never get these crashes considering the sample size…
You can use the above as a temp fix. I’ll work on an experimental fix, test it, and report back.
After futher investigation, I found the reason of my crashes. In short: we change
collectionViewin adapter inSectionControllerthat createsEmbeddedTableViewCell:Debugging process
I prepared function that periodically and every second returns 3, 4 or 8 elements. After few updates I receive crash:
Under the hood
Function
numberOfSectionsInCollectionViewinIGListAdapter+UICollectionView.m(implementsUICollectionViewDataSource) returns 3, 4 or 8 element but for different instances ofUICollectionView. After my experiments I know IGListKit prepares 2 different cells for one element (ListAdapterDataSource for RailSectionController returns oneEmbeddedCollectionViewCell). As a result every call ofcellForRowsection controller changecollectionViewin adapter. When adapter getsperformUpdates(animated:it perform inserts and deletes but not always on the same collection view. When it happens the collection view is not up-to-date. For example it might “think” it has 4 elements, but we want to delete 5, 6, 7 and 8 cell.I was able to crash iOS example with minimum changes:
didUpdateTohttps://github.com/rafalszastok/IGListKit/tree/bug/crash-example
@rnystrom guess you wrongly tagged me. instead of @rafalszastok 😆
@rnystrom Is there a chance to have first-class embedded controller in the 3.3.0 release?
It’s already taken to version 3.2.0: https://github.com/Instagram/IGListKit/milestone/6
Facing the same issue in our app, would welcome “first-class embedded controller”!