Skip to content

Instantly share code, notes, and snippets.

@akisute
Created February 3, 2015 05:55
Show Gist options
  • Save akisute/ee17463f862a334731c4 to your computer and use it in GitHub Desktop.
Save akisute/ee17463f862a334731c4 to your computer and use it in GitHub Desktop.
TaskToken to cancel running tasks with BFTask
- (BFTask *)loadSomeDataWithToken:(TaskToken *)token
{
BFTask *getSomeDataTask;
BFTask *getOtherDataTask;
getSomeDataTask = [[APIClient sharedClient] withToken:token executeAPI:^BFTask *(APIClient *client) {
return [[client getSomeDataWithParameters:@{}] continueWithSuccessBlock:^id(BFTask *task) {
// TODO: do something
return task;
}];
}];
getSomeDataTask = [[APIClient sharedClient] withToken:token executeAPI:^BFTask *(APIClient *client) {
return [[client getOtherDataWithParameters:@{@"user": @"akisute"}] continueWithSuccessBlock:^id(BFTask *task) {
// TODO: do other thing
return task;
}];
}];
return [BFTask taskForCompletionOfAllTasks:@[getSomeDataTask, getOtherDataTask]];
}
- (void)doSomethingInSomewhere
{
self.token = [TaskToken token];
[[self loadSomeDataWithToken:token] continueWithSuccessBlock:^id(BFTask *task) {
// TODO: update data
self.token = nil;
return task;
}];
}
- (void)cancelLoading
{
if (self.token) {
[self.token cancel];
self.token = nil;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment