From 88b36f58e7e9bfa9c9c3053a9841276487c562be Mon Sep 17 00:00:00 2001 From: Scott Goodson Date: Sun, 6 Dec 2015 21:45:46 -0800 Subject: [PATCH] Ensure an empty array is returned if visibleNodes is called before any nodes are complete. --- AsyncDisplayKit/ASCollectionView.mm | 16 ++++++++++------ AsyncDisplayKit/ASTableView.mm | 13 ++++++++----- AsyncDisplayKit/Details/ASDataController.mm | 15 ++++++++++++++- .../Sample.xcodeproj/project.pbxproj | 16 ---------------- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index 7c5e5fabac..ffd1c66005 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -391,15 +391,19 @@ static BOOL _isInterceptedSelector(SEL sel) { NSArray *indexPaths = [self indexPathsForVisibleItems]; NSMutableArray *visibleNodes = [[NSMutableArray alloc] init]; - - [indexPaths enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - ASCellNode *visibleNode = [self nodeForItemAtIndexPath:obj]; - [visibleNodes addObject:visibleNode]; - }]; - + + for (NSIndexPath *indexPath in indexPaths) { + ASCellNode *node = [self nodeForItemAtIndexPath:indexPath]; + if (node) { + // It is possible for UICollectionView to return indexPaths before the node is completed. + [visibleNodes addObject:node]; + } + } + return visibleNodes; } + #pragma mark Assertions. - (void)performBatchAnimated:(BOOL)animated updates:(void (^)())updates completion:(void (^)(BOOL))completion diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 751247d7a0..2c73367e96 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -379,11 +379,14 @@ static BOOL _isInterceptedSelector(SEL sel) NSArray *indexPaths = [self indexPathsForVisibleRows]; NSMutableArray *visibleNodes = [[NSMutableArray alloc] init]; - [indexPaths enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - ASCellNode *visibleNode = [self nodeForRowAtIndexPath:obj]; - [visibleNodes addObject:visibleNode]; - }]; - + for (NSIndexPath *indexPath in indexPaths) { + ASCellNode *node = [self nodeForRowAtIndexPath:indexPath]; + if (node) { + // It is possible for UITableView to return indexPaths before the node is completed. + [visibleNodes addObject:node]; + } + } + return visibleNodes; } diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index e9a728c42e..72b676a7de 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -902,7 +902,20 @@ static void *kASSizingQueueContext = &kASSizingQueueContext; - (ASCellNode *)nodeAtIndexPath:(NSIndexPath *)indexPath { ASDisplayNodeAssertMainThread(); - return [self completedNodes][indexPath.section][indexPath.row]; + + NSArray *completedNodes = [self completedNodes]; + NSInteger section = indexPath.section; + NSInteger row = indexPath.row; + ASCellNode *node = nil; + + if (section >= 0 && row >= 0 && section < completedNodes.count) { + NSArray *completedNodesSection = completedNodes[section]; + if (row < completedNodesSection.count) { + node = completedNodesSection[row]; + } + } + + return node; } - (NSIndexPath *)indexPathForNode:(ASCellNode *)cellNode; diff --git a/examples/ASTableViewStressTest/Sample.xcodeproj/project.pbxproj b/examples/ASTableViewStressTest/Sample.xcodeproj/project.pbxproj index 30be626c5c..e066c2fcd1 100644 --- a/examples/ASTableViewStressTest/Sample.xcodeproj/project.pbxproj +++ b/examples/ASTableViewStressTest/Sample.xcodeproj/project.pbxproj @@ -118,7 +118,6 @@ 05E2127E19D4DB510098F589 /* Frameworks */, 05E2127F19D4DB510098F589 /* Resources */, F012A6F39E0149F18F564F50 /* Copy Pods Resources */, - E671F9E92DFB9088485E493B /* Embed Pods Frameworks */, ); buildRules = ( ); @@ -190,21 +189,6 @@ shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; showEnvVarsInLog = 0; }; - E671F9E92DFB9088485E493B /* Embed Pods Frameworks */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "Embed Pods Frameworks"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods/Pods-frameworks.sh\"\n"; - showEnvVarsInLog = 0; - }; F012A6F39E0149F18F564F50 /* Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647;