[ASDataController] Call All Completion Handlers after Nested Batch Updates (#2274)

* Add tests for batch update completion handler calling

* Ensure we call all completion handlers after collection view updates

* Tweak it

* Fix the doc

* Minor improvements

* Document addCompletionHandler better
This commit is contained in:
Adlai Holler
2016-09-27 17:21:28 -04:00
committed by GitHub
parent 416d8f92e1
commit ec64b9b229
7 changed files with 147 additions and 10 deletions

View File

@@ -20,6 +20,11 @@
_ASHierarchyChangeSet *_changeSet;
}
- (void)dealloc
{
ASDisplayNodeCAssert(_changeSetBatchUpdateCounter == 0, @"ASChangeSetDataController deallocated in the middle of a batch update.");
}
#pragma mark - Batching (External API)
- (void)beginUpdates
@@ -40,18 +45,20 @@
// Prevent calling endUpdatesAnimated:completion: in an unbalanced way
NSAssert(_changeSetBatchUpdateCounter >= 0, @"endUpdatesAnimated:completion: called without having a balanced beginUpdates call");
[_changeSet addCompletionHandler:completion];
if (_changeSetBatchUpdateCounter == 0) {
[self invalidateDataSourceItemCounts];
[_changeSet markCompletedWithNewItemCounts:[self itemCountsFromDataSource]];
void (^batchCompletion)(BOOL finished) = _changeSet.completionHandler;
if (!self.initialReloadDataHasBeenCalled) {
if (completion) {
completion(YES);
if (batchCompletion != nil) {
batchCompletion(YES);
}
_changeSet = nil;
return;
}
[self invalidateDataSourceItemCounts];
[_changeSet markCompletedWithNewItemCounts:[self itemCountsFromDataSource]];
[super beginUpdates];
for (_ASHierarchyItemChange *change in [_changeSet itemChangesOfType:_ASHierarchyChangeTypeDelete]) {
@@ -70,7 +77,7 @@
[super insertRowsAtIndexPaths:change.indexPaths withAnimationOptions:change.animationOptions];
}
[super endUpdatesAnimated:animated completion:completion];
[super endUpdatesAnimated:animated completion:batchCompletion];
_changeSet = nil;
}