From b6bf5289a2964a665ae34b04d6e2af031e097889 Mon Sep 17 00:00:00 2001 From: Diego Sanchez Date: Tue, 22 Mar 2016 14:41:44 +0000 Subject: [PATCH] Tentative fix for crash in UICollectionView.performBatchUpdates --- .../BaseChatViewController+Changes.swift | 14 ++++++-------- .../ChatController/ChatCollectionViewLayout.swift | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift index add5ace..e666fc8 100644 --- a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift +++ b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift @@ -130,14 +130,13 @@ extension BaseChatViewController: ChatDataSourceDelegateProtocol { if updateType == .Normal { UIView.animateWithDuration(self.constants.updatesAnimationDuration, animations: { () -> Void in - // We want to update visible cells to support easy removal of bubble tail or any other updates that may be needed after a data update - // Collection view state is not constistent after performBatchUpdates. It can happen that we ask a cell for an index path and we still get the old one. - // Visible cells can be either updated in completion block (easier but with delay) or before, taking into account if some cell is gonna be moved - - updateModelClosure() - self.updateVisibleCells(changes) - self.collectionView.performBatchUpdates({ () -> Void in + // We want to update visible cells to support easy removal of bubble tail or any other updates that may be needed after a data update + // Collection view state is not constistent after performBatchUpdates. It can happen that we ask a cell for an index path and we still get the old one. + // Visible cells can be either updated in completion block (easier but with delay) or before, taking into account if some cell is gonna be moved + updateModelClosure() + self.updateVisibleCells(changes) + self.collectionView.deleteItemsAtIndexPaths(Array(changes.deletedIndexPaths)) self.collectionView.insertItemsAtIndexPaths(Array(changes.insertedIndexPaths)) for move in changes.movedIndexPaths { @@ -269,7 +268,6 @@ extension BaseChatViewController: ChatDataSourceDelegateProtocol { public func chatCollectionViewLayoutModel() -> ChatCollectionViewLayoutModel { if self.layoutModel.calculatedForWidth != self.collectionView.bounds.width { self.layoutModel = self.createLayoutModel(self.chatItemCompanionCollection, collectionViewWidth: self.collectionView.bounds.width) - } return self.layoutModel } diff --git a/Chatto/Source/ChatController/ChatCollectionViewLayout.swift b/Chatto/Source/ChatController/ChatCollectionViewLayout.swift index 3a0a811..2504950 100644 --- a/Chatto/Source/ChatController/ChatCollectionViewLayout.swift +++ b/Chatto/Source/ChatController/ChatCollectionViewLayout.swift @@ -81,6 +81,7 @@ public class ChatCollectionViewLayout: UICollectionViewLayout { guard let delegate = self.delegate else { return } var oldLayoutModel = self.layoutModel self.layoutModel = delegate.chatCollectionViewLayoutModel() + self.layoutNeedsUpdate = false dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in // Dealloc of layout with 5000 items take 25 ms on tests on iPhone 4s // This moves dealloc out of main thread