Files
AsyncDisplayKit/_docs/batch-fetching-api.md
Hannah Troisi d162141e18 # This is a combination of 44 commits.
# The first commit's message is:
# This is a combination of 30 commits.
# The first commit's message is:
Update nav_docs.yml

# This is the 2nd commit message:

Create node-containers-overview.md

# This is the 3rd commit message:

Update node-containers-overview.md

# This is the 4th commit message:

Update node-containers-overview.md

# This is the 5th commit message:

Update node-containers-overview.md

# This is the 6th commit message:

Update node-containers-overview.md

# This is the 7th commit message:

Update nav_docs.yml

# This is the 8th commit message:

Update nav_docs.yml

# This is the 9th commit message:

Update and rename node-containers-overview.md to containers-overview.md

# This is the 10th commit message:

Update and rename asviewcontroller.md to container-asviewcontroller.md

# This is the 11th commit message:

Update and rename astablenode.md to container-astablenode.md

# This is the 12th commit message:

Update and rename ascollectionnode.md to container-ascollectionnode.md

# This is the 13th commit message:

Update and rename aspagernode.md to container-aspagernode.md

# This is the 14th commit message:

Update and rename container-ascollectionnode.md to containers-ascollectionnode.md

# This is the 15th commit message:

Update and rename container-asviewcontroller.md to containers-asviewcontroller.md

# This is the 16th commit message:

Update containers-ascollectionnode.md

# This is the 17th commit message:

Update container-astablenode.md

# This is the 18th commit message:

Rename container-astablenode.md to containers-astablenode.md

# This is the 19th commit message:

Update and rename container-aspagernode.md to containers-aspagernode.md

# This is the 20th commit message:

Update containers-overview.md

# This is the 21st commit message:

Update containers-overview.md

# This is the 22nd commit message:

Update containers-overview.md

# This is the 23rd commit message:

Update containers-asviewcontroller.md

# This is the 24th commit message:

Update containers-asviewcontroller.md

# This is the 25th commit message:

Update containers-overview.md

# This is the 26th commit message:

Update containers-asviewcontroller.md

# This is the 27th commit message:

Update containers-asviewcontroller.md

# This is the 28th commit message:

Update containers-asviewcontroller.md

# This is the 29th commit message:

Update containers-overview.md

# This is the 30th commit message:

Update containers-overview.md

# This is the 2nd commit message:

Removing _site folder from repository (only needed to run locally)

# This is the 3rd commit message:

Update containers-overview.md

# This is the 4th commit message:

Update containers-overview.md

# This is the 5th commit message:

Update containers-asviewcontroller.md

# This is the 6th commit message:

Update debug-tool-pixel-scaling.md

# This is the 7th commit message:

Update debug-tool-pixel-scaling.md

# This is the 8th commit message:

Update debug-tool-pixel-scaling.md

# This is the 9th commit message:

Update debug-tool-pixel-scaling.md

# This is the 10th commit message:

Update debug-tool-pixel-scaling.md

# This is the 11th commit message:

Update debug-tool-pixel-scaling.md

# This is the 12th commit message:

Update debug-tool-pixel-scaling.md

# This is the 13th commit message:

Update debug-tool-hit-test-slop.md

# This is the 14th commit message:

Update debug-tool-hit-test-slop.md

# This is the 15th commit message:

Update nav_docs.yml

# This is the 16th commit message:

Update image-node.md

# This is the 17th commit message:

Update image-node.md

# This is the 18th commit message:

Update control-node.md

# This is the 19th commit message:

Update control-node.md

# This is the 20th commit message:

Update nav_docs.yml

# This is the 21st commit message:

Create batch-fetching-api.md

# This is the 22nd commit message:

Update batch-fetching-api.md

# This is the 23rd commit message:

Update debug-tool-hit-test-slop.md

# This is the 24th commit message:

Update debug-tool-pixel-scaling.md

# This is the 25th commit message:

Update debug-tool-hit-test-slop.md

# This is the 26th commit message:

Update debug-tool-pixel-scaling.md

# This is the 27th commit message:

Update containers-aspagernode.md

# This is the 28th commit message:

Update containers-aspagernode.md

# This is the 29th commit message:

Update containers-aspagernode.md

# This is the 30th commit message:

Update containers-aspagernode.md

# This is the 31st commit message:

Update containers-asviewcontroller.md

# This is the 32nd commit message:

Update containers-asviewcontroller.md

# This is the 33rd commit message:

Update containers-asviewcontroller.md

# This is the 34th commit message:

Update containers-asviewcontroller.md

# This is the 35th commit message:

Update containers-overview.md

# This is the 36th commit message:

Update containers-asviewcontroller.md

# This is the 37th commit message:

Update containers-astablenode.md

# This is the 38th commit message:

Update containers-astablenode.md

# This is the 39th commit message:

Update containers-astablenode.md

# This is the 40th commit message:

Update containers-astablenode.md

# This is the 41st commit message:

Update containers-astablenode.md

# This is the 42nd commit message:

Update containers-astablenode.md

# This is the 43rd commit message:

Update containers-astablenode.md

# This is the 44th commit message:

Update containers-astablenode.md
2016-04-19 00:54:31 -07:00

4.0 KiB

title, layout, permalink, next
title layout permalink next
Batch Fetching API docs /docs/batch-fetching-api.html image-modification-block.html

AsyncDisplayKit's Batch Fetching API makes it easy for developers to add fetching of new data in chunks. In case the user scrolled to a specific range of a table or collection view the automatic batch fetching mechanism of ASDK kicks in.

You as a developer can define the point when the batch fetching mechanism should start via the leadingScreensForBatching property on an ASTableView or ASCollectionView. The default value for this property is 2.0.

To support batch fetching you have to implement two methods in your ASTableView or ASCollectionView delegate object: The first method you have to implement is for ASTableView delegate:

- (BOOL)shouldBatchFetchForTableView:(ASTableView *)tableView

or for ASCollectionView delegate:

- (BOOL)shouldBatchFetchForCollectionView:(ASCollectionView *)collectionView

In this method you have decide if the batch fetching mechanism should kick in if the user scrolled in batch fetching range or not. Usually this decision is based on if there is still data to fetch or not, based on e.g. previous API calls or some local dataset operations.

If you return NO from - (BOOL)shouldBatchFetchForCollectionView:(ASCollectionView *)collectionView, no new batch fetching process will happen, in case you return YES the batch fetching mechanism will start and the following method is called for your ASTableView delegate:

- (void)tableView:(ASTableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context;

or for ASCollectionView delegate:

- (void)collectionView:(ASCollectionView *)collectionView willBeginBatchFetchWithContext:(ASBatchContext *)context;

First of all, you have to be careful within this method as it's called on a background thread. If you have to do anything on the main thread, you are responsible for dispatching it to the main thread and proceed with work you have to do in process to finish the batch fetch.

Within - (void)collectionView:(ASCollectionView *)collectionView willBeginBatchFetchWithContext:(ASBatchContext *)context; you should do any necessary steps to fetch the next chunk of data e.g. from a local database, an API etc.

After you finished fetching the next chunk of data, it is very important to let ASDK know that you finished the process. To do that you have to call completeBatchFetching: on the context object that was passed in with a parameter value of YES. This assures that the whole batch fetching mechanism stays in sync and a next batch fetching cycle can happen. Only by passing YES will the context know to attempt another batch update when necessary. If you pass in NO nothing will happen.

Here you can see an example how a batch fetching cycle could look like:

- (BOOL)shouldBatchFetchForTableView:(ASTableView *)tableView 
{
  // Decide if the batch fetching mechanism should kick in
  if (_stillDataToFetch) {
    return YES;
  }
  return NO;
}

- (void)tableView:(ASTableView *)tableView willBeginBatchFetchWithContext:(ASBatchContext *)context 
{
  // Fetch data most of the time asynchronoulsy from an API or local database
  NSArray *data = ...;

  // Insert data into table or collection view
  [self insertNewRowsInTableView:newPhotos];

  // Decide if it's still necessary to trigger more batch fetches in the future
  _stillDataToFetch = ...;

  // Properly finish the batch fetch
  [context completeBatchFetching:YES]
}

Check out the following sample apps to see the batch fetching API implemented within an app: