[ASRangeController] Don't bother asking UIKit for the visible index paths if view is zero-sized, as it triggers a reloadData.

This commit is contained in:
Scott Goodson 2016-03-10 19:10:25 -08:00
parent aab2ecc26e
commit 0e460ca00a
3 changed files with 21 additions and 5 deletions

View File

@ -867,7 +867,10 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (NSArray *)visibleNodeIndexPathsForRangeController:(ASRangeController *)rangeController - (NSArray *)visibleNodeIndexPathsForRangeController:(ASRangeController *)rangeController
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
return [self indexPathsForVisibleItems]; // Calling visibleNodeIndexPathsForRangeController: will trigger UIKit to call reloadData if it never has, which can result
// in incorrect layout if performed at zero size. We can use the fact that nothing can be visible at zero size to return fast.
BOOL isZeroSized = CGRectEqualToRect(self.bounds, CGRectZero);
return isZeroSized ? @[] : [self indexPathsForVisibleItems];
} }
- (CGSize)viewportSizeForRangeController:(ASRangeController *)rangeController - (CGSize)viewportSizeForRangeController:(ASRangeController *)rangeController

View File

@ -718,6 +718,12 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
{ {
ASDisplayNodeAssertMainThread(); ASDisplayNodeAssertMainThread();
// Calling indexPathsForVisibleRows will trigger UIKit to call reloadData if it never has, which can result
// in incorrect layout if performed at zero size. We can use the fact that nothing can be visible at zero size to return fast.
if (CGRectEqualToRect(self.bounds, CGRectZero)) {
return @[];
}
NSArray *visibleIndexPaths = self.indexPathsForVisibleRows; NSArray *visibleIndexPaths = self.indexPathsForVisibleRows;
if (_pendingVisibleIndexPath) { if (_pendingVisibleIndexPath) {

View File

@ -149,6 +149,17 @@ static UIApplicationState __ApplicationState = UIApplicationStateActive;
return; return;
} }
// allNodes is a 2D array: it contains arrays for each section, each containing nodes.
NSArray<NSArray *> *allNodes = [_dataSource completedNodes];
NSUInteger numberOfSections = [allNodes count];
if (_allPreviousIndexPaths.count == 0 && allNodes.count == 0) {
// In certain cases, such as on app suspend, an update may be triggered before we've loaded anything.
// For example, an ASCollectionNode inside another scrollable area will not load content until it has entered
// the display range, but the object may have been allocated by a cell and added to the set of active range controllers.
return;
}
// TODO: Consider if we need to use this codepath, or can rely on something more similar to the data & display ranges // TODO: Consider if we need to use this codepath, or can rely on something more similar to the data & display ranges
// Example: ... = [_layoutController indexPathsForScrolling:_scrollDirection rangeType:ASLayoutRangeTypeVisible]; // Example: ... = [_layoutController indexPathsForScrolling:_scrollDirection rangeType:ASLayoutRangeTypeVisible];
NSArray<NSIndexPath *> *visibleNodePaths = [_dataSource visibleNodeIndexPathsForRangeController:self]; NSArray<NSIndexPath *> *visibleNodePaths = [_dataSource visibleNodeIndexPathsForRangeController:self];
@ -165,10 +176,6 @@ static UIApplicationState __ApplicationState = UIApplicationStateActive;
[_layoutController setVisibleNodeIndexPaths:visibleNodePaths]; [_layoutController setVisibleNodeIndexPaths:visibleNodePaths];
} }
// allNodes is a 2D array: it contains arrays for each section, each containing nodes.
NSArray<NSArray *> *allNodes = [_dataSource completedNodes];
NSUInteger numberOfSections = [allNodes count];
NSArray<ASDisplayNode *> *currentSectionNodes = nil; NSArray<ASDisplayNode *> *currentSectionNodes = nil;
NSInteger currentSectionIndex = -1; // Set to -1 so we don't match any indexPath.section on the first iteration. NSInteger currentSectionIndex = -1; // Set to -1 so we don't match any indexPath.section on the first iteration.
NSUInteger numberOfNodesInSection = 0; NSUInteger numberOfNodesInSection = 0;