diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index 4463ae6090..256ae11038 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -386,11 +386,12 @@ static BOOL _isInterceptedSelector(SEL sel) - (BOOL)shouldBatchFetch { - if ([_asyncDelegate respondsToSelector:@selector(shouldBatchFetchForCollectionView:)]) { + // if the delegate does not respond to this method, there is no point in starting to fetch + BOOL canFetch = [_asyncDelegate respondsToSelector:@selector(collectionView:willBeginBatchFetchWithContext:)]; + if (canFetch && [_asyncDelegate respondsToSelector:@selector(shouldBatchFetchForCollectionView:)]) { return [_asyncDelegate shouldBatchFetchForCollectionView:self]; } else { - // if the delegate does not respond to this method, there is no point in starting to fetch - return [_asyncDelegate respondsToSelector:@selector(collectionView:willBeginBatchFetchWithContext:)]; + return canFetch; } } diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index 6ccf51fd77..d34886576b 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -392,11 +392,12 @@ static BOOL _isInterceptedSelector(SEL sel) - (BOOL)shouldBatchFetch { - if ([_asyncDelegate respondsToSelector:@selector(shouldBatchFetchForTableView:)]) { + // if the delegate does not respond to this method, there is no point in starting to fetch + BOOL canFetch = [_asyncDelegate respondsToSelector:@selector(tableView:willBeginBatchFetchWithContext:)]; + if (canFetch && [_asyncDelegate respondsToSelector:@selector(shouldBatchFetchForTableView:)]) { return [_asyncDelegate shouldBatchFetchForTableView:self]; } else { - // if the delegate does not respond to this method, there is no point in starting to fetch - return [_asyncDelegate respondsToSelector:@selector(tableView:willBeginBatchFetchWithContext:)]; + return canFetch; } } diff --git a/AsyncDisplayKit/Details/ASBatchFetching.m b/AsyncDisplayKit/Details/ASBatchFetching.m index c0233cc626..c4027a70c5 100644 --- a/AsyncDisplayKit/Details/ASBatchFetching.m +++ b/AsyncDisplayKit/Details/ASBatchFetching.m @@ -19,16 +19,14 @@ BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context, return NO; } - // no fetching for null states - if (leadingScreens <= 0.0 || - CGPointEqualToPoint(targetOffset, CGPointZero) || - CGSizeEqualToSize(contentSize, CGSizeZero) || - CGRectEqualToRect(bounds, CGRectZero)) { + // only Up and Left scrolls are currently supported (tail loading) + if (scrollDirection != ASScrollDirectionUp && scrollDirection != ASScrollDirectionLeft) { return NO; } - // only Up and Left scrolls are currently supported (tail loading) - if (scrollDirection != ASScrollDirectionUp && scrollDirection != ASScrollDirectionLeft) { + // no fetching for null states + if (leadingScreens <= 0.0 || + CGRectEqualToRect(bounds, CGRectZero)) { return NO; } @@ -44,8 +42,11 @@ BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context, contentLength = contentSize.width; } + // target offset will always be 0 if the content size is smaller than the viewport + BOOL hasSmallContent = offset == 0.0 && contentLength < viewLength; + CGFloat triggerDistance = viewLength * leadingScreens; CGFloat remainingDistance = contentLength - viewLength - offset; - return remainingDistance <= triggerDistance; + return hasSmallContent || remainingDistance <= triggerDistance; } diff --git a/AsyncDisplayKitTests/ASBatchFetchingTests.m b/AsyncDisplayKitTests/ASBatchFetchingTests.m index 48f367e223..3b009cdda3 100644 --- a/AsyncDisplayKitTests/ASBatchFetchingTests.m +++ b/AsyncDisplayKitTests/ASBatchFetchingTests.m @@ -99,4 +99,20 @@ XCTAssert(shouldFetch == YES, @"Fetch should begin when vertically scrolling past the content size"); } +- (void)testVerticalScrollingSmallContentSize { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // when the content size is < screen size, the target offset will always be 0 + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionUp, VERTICAL_RECT(screen), VERTICAL_SIZE(screen * 0.5), VERTICAL_OFFSET(0.0), 1.0); + XCTAssert(shouldFetch == YES, @"Fetch should begin when the target is 0 and the content size is smaller than the scree"); +} + +- (void)testHorizontalScrollingSmallContentSize { + CGFloat screen = 1.0; + ASBatchContext *context = [[ASBatchContext alloc] init]; + // when the content size is < screen size, the target offset will always be 0 + BOOL shouldFetch = ASDisplayShouldFetchBatchForContext(context, ASScrollDirectionLeft, HORIZONTAL_RECT(screen), HORIZONTAL_SIZE(screen * 0.5), HORIZONTAL_OFFSET(0.0), 1.0); + XCTAssert(shouldFetch == YES, @"Fetch should begin when the target is 0 and the content size is smaller than the scree"); +} + @end diff --git a/examples/ASCollectionView/Sample/ViewController.m b/examples/ASCollectionView/Sample/ViewController.m index 7b8bfcdaf9..5e2ab4292a 100644 --- a/examples/ASCollectionView/Sample/ViewController.m +++ b/examples/ASCollectionView/Sample/ViewController.m @@ -89,7 +89,7 @@ // unlock the data source to enable data source updating. } -- (void)collectionView:(UICollectionView *)collectionView beginBatchFetchingWithContext:(ASBatchContext *)context +- (void)collectionView:(UICollectionView *)collectionView willBeginBatchFetchWithContext:(ASBatchContext *)context { NSLog(@"fetch additional content"); [context completeBatchFetching:YES]; diff --git a/examples/Kittens/Sample/ViewController.m b/examples/Kittens/Sample/ViewController.m index 5433b4a01d..f9c8838470 100644 --- a/examples/Kittens/Sample/ViewController.m +++ b/examples/Kittens/Sample/ViewController.m @@ -55,7 +55,7 @@ static const NSInteger kMaxLitterSize = 100; // populate our "data source" with some random kittens - _kittenDataSource = [self createLitterWithSize:kLitterSize];; + _kittenDataSource = [self createLitterWithSize:kLitterSize]; return self; } @@ -63,7 +63,7 @@ static const NSInteger kMaxLitterSize = 100; - (NSArray *)createLitterWithSize:(NSInteger)litterSize { NSMutableArray *kittens = [NSMutableArray arrayWithCapacity:litterSize]; - for (NSInteger i = 0; i < kLitterSize; i++) { + for (NSInteger i = 0; i < litterSize; i++) { u_int32_t deltaX = arc4random_uniform(10) - 5; u_int32_t deltaY = arc4random_uniform(10) - 5; CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY); @@ -140,7 +140,7 @@ static const NSInteger kMaxLitterSize = 100; return _kittenDataSource.count < kMaxLitterSize; } -- (void)tableView:(UITableView *)tableView beginBatchFetchingWithContext:(ASBatchContext *)context +- (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context { NSLog(@"adding kitties"); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{