IGListKit/Source/IGListReloadDataUpdater.m
Ryan Nystrom 073fc073e0 Prevent duplicate item deletes and drop reload collisions
Summary:
a415ef5552 exposed a bug in `UICollectionView` where its state gets corrupted when deleting the same index path more than once in a single batch update block. This resulted crashes like

```
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x4)
Closes https://github.com/Instagram/IGListKit/pull/657

Reviewed By: jessesquires

Differential Revision: D4913790

Pulled By: rnystrom

fbshipit-source-id: 8f6fcdd2e2438da309fc64ca0ac111b9a0980149
2017-04-19 17:17:41 -07:00

81 lines
3.2 KiB
Objective-C

/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import <IGListKit/IGListReloadDataUpdater.h>
@implementation IGListReloadDataUpdater
#pragma mark - IGListUpdatingDelegate
- (NSPointerFunctions *)objectLookupPointerFunctions {
return [NSPointerFunctions pointerFunctionsWithOptions:NSPointerFunctionsObjectPersonality];
}
- (void)performUpdateWithCollectionView:(UICollectionView *)collectionView
fromObjects:(NSArray *)fromObjects
toObjects:(NSArray *)toObjects
animated:(BOOL)animated
objectTransitionBlock:(IGListObjectTransitionBlock)objectTransitionBlock
completion:(IGListUpdatingCompletion)completion {
objectTransitionBlock(toObjects);
[self synchronousReloadDataWithCollectionView:collectionView];
if (completion) {
completion(YES);
}
}
- (void)performUpdateWithCollectionView:(UICollectionView *)collectionView
animated:(BOOL)animated
itemUpdates:(IGListItemUpdateBlock)itemUpdates
completion:(IGListUpdatingCompletion)completion {
itemUpdates();
[self synchronousReloadDataWithCollectionView:collectionView];
if (completion) {
completion(YES);
}
}
- (void)insertItemsIntoCollectionView:(UICollectionView *)collectionView indexPaths:(NSArray<NSIndexPath *> *)indexPaths {
[self synchronousReloadDataWithCollectionView:collectionView];
}
- (void)deleteItemsFromCollectionView:(UICollectionView *)collectionView indexPaths:(NSArray<NSIndexPath *> *)indexPaths {
[self synchronousReloadDataWithCollectionView:collectionView];
}
- (void)moveItemInCollectionView:(UICollectionView *)collectionView fromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
[self synchronousReloadDataWithCollectionView:collectionView];
}
- (void)reloadItemInCollectionView:(UICollectionView *)collectionView fromIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
[self synchronousReloadDataWithCollectionView:collectionView];
}
- (void)reloadItemsInCollectionView:(UICollectionView *)collectionView indexPaths:(NSArray<NSIndexPath *> *)indexPaths {
[self synchronousReloadDataWithCollectionView:collectionView];
}
- (void)reloadDataWithCollectionView:(UICollectionView *)collectionView reloadUpdateBlock:(IGListReloadUpdateBlock)reloadUpdateBlock completion:(IGListUpdatingCompletion)completion {
reloadUpdateBlock();
[self synchronousReloadDataWithCollectionView:collectionView];
if (completion) {
completion(YES);
}
}
- (void)reloadCollectionView:(UICollectionView *)collectionView sections:(NSIndexSet *)sections {
[self synchronousReloadDataWithCollectionView:collectionView];
}
- (void)synchronousReloadDataWithCollectionView:(UICollectionView *)collectionView {
[collectionView reloadData];
[collectionView layoutIfNeeded];
}
@end