Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in performBatchUpdates #71

Closed
diegosanchezr opened this issue Mar 22, 2016 · 8 comments
Closed

Crash in performBatchUpdates #71

diegosanchezr opened this issue Mar 22, 2016 · 8 comments

Comments

@diegosanchezr
Copy link
Contributor

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0x1cc284a0> {length = 2, path = 0 - 114}'

Seen on iOS 8. No steps to reproduce known.

Last Exception Backtrace:
0   CoreFoundation                       0x2938bf8f __exceptionPreprocess + 127
1   libobjc.A.dylib                      0x37a3cc8b objc_exception_throw + 36
2   CoreFoundation                       0x2938be65 +[NSException raise:format:arguments:] + 102
3   Foundation                           0x2a090f17 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 88
4   UIKit                                0x2ca662c7 __45-[UICollectionViewData validateLayoutInRect:]_block_invoke + 800
5   UIKit                                0x2ca65b1f -[UICollectionViewData validateLayoutInRect:] + 1736
6   UIKit                                0x2ca651eb -[UICollectionView layoutSubviews] + 168
7   UIKit                                0x2ca0ae43 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 544
8   QuartzCore                           0x2c41ed29 -[CALayer layoutSublayers] + 126
9   QuartzCore                           0x2c41a55d CA::Layer::layout_if_needed(CA::Transaction*) + 358
10  UIKit                                0x2ca1da23 -[UIView(Hierarchy) layoutBelowIfNeeded] + 136
11  UIKit                                0x2d0116dd -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 162
12  UIKit                                0x2d011633 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 28
13  UIKit                                0x2cbce467 -[UICollectionView performBatchUpdates:completion:] + 28
14  Chatto                               0x01549120 Chatto.BaseChatViewController.(performBatchUpdates (Chatto.BaseChatViewController) -> (updateModelClosure : () -> (), changes : Chatto.CollectionChanges, updateType : Chatto.UpdateType, completion : () -> ()) -> ()).(closure #2) (BaseChatViewController+Changes.swift:148)
15  Chatto                               0x015471b8 partial apply forwarder for Chatto.BaseChatViewController.(performBatchUpdates (Chatto.BaseChatViewController) -> (updateModelClosure : () -> (), changes : Chatto.CollectionChanges, updateType : Chatto.UpdateType, completion : () -> ()) -> ()).(closure #2) (BaseChatViewController+Changes.swift:0)
16  UIKit                                0x2ca3efb7 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 480
17  UIKit                                0x2ca3edcb +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:] + 64
18  Chatto                               0x01544314 function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Exploded> of Chatto.BaseChatViewController.performBatchUpdates (Chatto.BaseChatViewController)(updateModelClosure : () -> (), changes : Chatto.CollectionChanges, updateType : Chatto.UpdateType, completion : () -> ()) -> () (BaseChatViewController+Changes.swift:149)
19  Chatto                               0x0154979c Chatto.BaseChatViewController.((updateModels in _612DFD8221191D415C81FF197E016366) (Chatto.BaseChatViewController) -> (newItems : [Chatto.ChatItemProtocol], oldItems : Chatto.ReadOnlyOrderedDictionary<Chatto.ChatItemCompanion>, updateType : Chatto.UpdateType, completion : () -> ()) -> ()).(closure #1) (BaseChatViewController+Changes.swift:179)
20  Chatto                               0x01546a68 partial apply forwarder for Chatto.BaseChatViewController.((updateModels in _612DFD8221191D415C81FF197E016366) (Chatto.BaseChatViewController) -> (newItems : [Chatto.ChatItemProtocol], oldItems : Chatto.ReadOnlyOrderedDictionary<Chatto.ChatItemCompanion>, updateType : Chatto.UpdateType, completion : () -> ()) -> ()).(closure #1) (BaseChatViewController+Changes.swift:0)
21  Chatto                               0x01547684 partial apply forwarder for Chatto.BaseChatViewController.((updateModels in _612DFD8221191D415C81FF197E016366) (Chatto.BaseChatViewController) -> (newItems : [Chatto.ChatItemProtocol], oldItems : Chatto.ReadOnlyOrderedDictionary<Chatto.ChatItemCompanion>, updateType : Chatto.UpdateType, completion : () -> ()) -> ()).(closure #3).(closure #1) (BaseChatViewController+Changes.swift:193)
22  libdispatch.dylib                    0x37fcd2e3 _dispatch_call_block_and_release + 8
23  libdispatch.dylib                    0x37fcd2cf _dispatch_client_callout + 20
24  libdispatch.dylib                    0x37fd0d2f _dispatch_main_queue_callback_4CF + 1328
25  CoreFoundation                       0x293513e9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 6
26  CoreFoundation                       0x2934fae9 __CFRunLoopRun + 1510
27  CoreFoundation                       0x2929bf31 CFRunLoopRunSpecific + 474
28  CoreFoundation                       0x2929bd43 CFRunLoopRunInMode + 104
29  GraphicsServices                     0x30d76201 GSEventRunModal + 134
30  UIKit                                0x2ca6d879 UIApplicationMain + 1438
@weyg
Copy link

weyg commented Mar 22, 2016

Seems crash occurs if you leave the ChatViewController while row add animation is in progress. Actually it only appears if you add random text messages to .Top and to .Bottom.

I've reproduced it here https://github.com/weyg/Chatto. (have a look at diff).

@diegosanchezr
Copy link
Contributor Author

@weyg That's awesome, thank you very much!. Fix is on the way :)

@weyg
Copy link

weyg commented Mar 22, 2016

Reproduced under iOS 8.1 and 9.2.

@diegosanchezr
Copy link
Contributor Author

I couldn't reproduce the crash with your code in dev branch. Assuming it's just trickier to get across. #72 should fix it

@weyg
Copy link

weyg commented Mar 22, 2016

Can't reproduce either. Seems that #72 really fixes the issue.

But now its much more slower than before. Main thread is used massively. (it happens after entering/leaving several chats).

But it really may be caused by some bad approach I'm using for appending/prepending messages.

@diegosanchezr
Copy link
Contributor Author

Yes, your NSTimer is retaining the ViewController so it keeps inserting items forever.

@weyg
Copy link

weyg commented Mar 22, 2016

Yes, your NSTimer is retaining the ViewController so it keeps inserting items forever.

Ignorant mistake:( Fixed it out and all works like a charm. Can't reproduce the crash with the same steps anymore.

@diegosanchezr any guesses about the reason of the crash?

@diegosanchezr
Copy link
Contributor Author

Looks like performBatchUpdates may trigger a layout of the UICollectionView before handling any changes. In that case, the UICollectionViewLayout can be also invalidated; but because we had updated the model it would return attributes for new items. Apple example also modifies the datasource inside the batch block (https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html#//apple_ref/doc/uid/TP40012334-CH7-SW7)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants