Merge pull request #72 from diegosanchezr/dev

Tentative fix for crash in UICollectionView.performBatchUpdates
This commit is contained in:
Diego Sánchez 2016-03-22 15:07:58 +00:00
commit d63ee00496
2 changed files with 7 additions and 8 deletions

View File

@ -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
}

View File

@ -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