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
{
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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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

View File

@@ -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];

View File

@@ -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), ^{