Batch fetch with small data sets

This commit is contained in:
Ryan Nystrom
2015-02-26 15:40:06 -08:00
parent 8d7fe334e2
commit c657411a2e
6 changed files with 37 additions and 18 deletions

View File

@@ -386,11 +386,12 @@ static BOOL _isInterceptedSelector(SEL sel)
- (BOOL)shouldBatchFetch - (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]; return [_asyncDelegate shouldBatchFetchForCollectionView:self];
} else { } else {
// if the delegate does not respond to this method, there is no point in starting to fetch return canFetch;
return [_asyncDelegate respondsToSelector:@selector(collectionView:willBeginBatchFetchWithContext:)];
} }
} }

View File

@@ -392,11 +392,12 @@ static BOOL _isInterceptedSelector(SEL sel)
- (BOOL)shouldBatchFetch - (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]; return [_asyncDelegate shouldBatchFetchForTableView:self];
} else { } else {
// if the delegate does not respond to this method, there is no point in starting to fetch return canFetch;
return [_asyncDelegate respondsToSelector:@selector(tableView:willBeginBatchFetchWithContext:)];
} }
} }

View File

@@ -19,16 +19,14 @@ BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context,
return NO; return NO;
} }
// no fetching for null states // only Up and Left scrolls are currently supported (tail loading)
if (leadingScreens <= 0.0 || if (scrollDirection != ASScrollDirectionUp && scrollDirection != ASScrollDirectionLeft) {
CGPointEqualToPoint(targetOffset, CGPointZero) ||
CGSizeEqualToSize(contentSize, CGSizeZero) ||
CGRectEqualToRect(bounds, CGRectZero)) {
return NO; return NO;
} }
// only Up and Left scrolls are currently supported (tail loading) // no fetching for null states
if (scrollDirection != ASScrollDirectionUp && scrollDirection != ASScrollDirectionLeft) { if (leadingScreens <= 0.0 ||
CGRectEqualToRect(bounds, CGRectZero)) {
return NO; return NO;
} }
@@ -44,8 +42,11 @@ BOOL ASDisplayShouldFetchBatchForContext(ASBatchContext *context,
contentLength = contentSize.width; 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 triggerDistance = viewLength * leadingScreens;
CGFloat remainingDistance = contentLength - viewLength - offset; CGFloat remainingDistance = contentLength - viewLength - offset;
return remainingDistance <= triggerDistance; return hasSmallContent || remainingDistance <= triggerDistance;
} }

View File

@@ -99,4 +99,20 @@
XCTAssert(shouldFetch == YES, @"Fetch should begin when vertically scrolling past the content size"); 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 @end

View File

@@ -89,7 +89,7 @@
// unlock the data source to enable data source updating. // 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"); NSLog(@"fetch additional content");
[context completeBatchFetching:YES]; [context completeBatchFetching:YES];

View File

@@ -55,7 +55,7 @@ static const NSInteger kMaxLitterSize = 100;
// populate our "data source" with some random kittens // populate our "data source" with some random kittens
_kittenDataSource = [self createLitterWithSize:kLitterSize];; _kittenDataSource = [self createLitterWithSize:kLitterSize];
return self; return self;
} }
@@ -63,7 +63,7 @@ static const NSInteger kMaxLitterSize = 100;
- (NSArray *)createLitterWithSize:(NSInteger)litterSize - (NSArray *)createLitterWithSize:(NSInteger)litterSize
{ {
NSMutableArray *kittens = [NSMutableArray arrayWithCapacity: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 deltaX = arc4random_uniform(10) - 5;
u_int32_t deltaY = arc4random_uniform(10) - 5; u_int32_t deltaY = arc4random_uniform(10) - 5;
CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY); CGSize size = CGSizeMake(350 + 2 * deltaX, 350 + 4 * deltaY);
@@ -140,7 +140,7 @@ static const NSInteger kMaxLitterSize = 100;
return _kittenDataSource.count < kMaxLitterSize; return _kittenDataSource.count < kMaxLitterSize;
} }
- (void)tableView:(UITableView *)tableView beginBatchFetchingWithContext:(ASBatchContext *)context - (void)tableView:(UITableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context
{ {
NSLog(@"adding kitties"); NSLog(@"adding kitties");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{