Prevent deallocation of asyncDataSource and asyncDelegate in ASCollectionView and ASTableView

Grab a strong reference for asyncDataSource and asyncDelegate in ASCollectionView and ASTableView before executing the range update to be sure they are not going away while executing the range update. This can happen in range updates while going back in the view controller hierarchy
This commit is contained in:
Michael Schneider
2016-03-22 08:09:59 -07:00
parent fbe87cde4b
commit 3668f45286
4 changed files with 48 additions and 4 deletions

View File

@@ -27,6 +27,7 @@
BOOL _didUpdateCurrentRange;
BOOL _didRegisterForNotifications;
CFAbsoluteTime _pendingDisplayNodesTimestamp;
NSMutableArray *_scheduledRangeUpdateCompletionBlocks;
}
@end
@@ -46,6 +47,7 @@ static UIApplicationState __ApplicationState = UIApplicationStateActive;
_rangeIsValid = YES;
_currentRangeMode = ASLayoutRangeModeInvalid;
_didUpdateCurrentRange = NO;
_scheduledRangeUpdateCompletionBlocks = [NSMutableArray array];
[[[self class] allRangeControllersWeakSet] addObject:self];
@@ -111,6 +113,15 @@ static UIApplicationState __ApplicationState = UIApplicationStateActive;
- (void)scheduleRangeUpdate
{
[self scheduleRangeUpdateCompletion:nil];
}
- (void)scheduleRangeUpdateCompletion:(void (^)(void))completion
{
if (completion) {
[_scheduledRangeUpdateCompletionBlocks addObject:completion];
}
if (_queuedRangeUpdate) {
return;
}
@@ -118,8 +129,13 @@ static UIApplicationState __ApplicationState = UIApplicationStateActive;
// coalesce these events -- handling them multiple times per runloop is noisy and expensive
_queuedRangeUpdate = YES;
__block id<ASRangeControllerDataSource> dataSource = _dataSource;
__block id<ASRangeControllerDelegate> delegate = _delegate;
dispatch_async(dispatch_get_main_queue(), ^{
[self performRangeUpdate];
[self _updateVisibleNodeIndexPaths];
dataSource = nil;
delegate = nil;
});
}
@@ -320,6 +336,11 @@ static UIApplicationState __ApplicationState = UIApplicationStateActive;
_rangeIsValid = YES;
_queuedRangeUpdate = NO;
for (void (^completionBlock)(void) in _scheduledRangeUpdateCompletionBlocks) {
completionBlock();
}
[_scheduledRangeUpdateCompletionBlocks removeAllObjects];
#if ASRangeControllerLoggingEnabled
// NSSet *visibleNodePathsSet = [NSSet setWithArray:visibleNodePaths];
// BOOL setsAreEqual = [visibleIndexPaths isEqualToSet:visibleNodePathsSet];