Skip to content

Commit

Permalink
Updated Readme.md - ##Task Cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
saniul committed Feb 21, 2014
1 parent 85480b9 commit e48ea14
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,32 @@ For common cases, such as dispatching on the main thread, we have provided defau
}];
```

## Task Cancellation

It's generally bad design to keep track of the `BFTaskCompletionSource` for cancellation. A better model is to create a "cancellation token" at the top level, and pass that to each async function that you want to be part of the same "cancelable operation". Then, in your continuation blocks, you can check whether the cancellation token has been cancelled and bail out early by returning a `[BFTask cancelledTask]`. For example:

```objective-c
- (void)doSomethingComplicatedAsync:(MYCancellationToken *)cancellationToken {
[[self doSomethingAsync:cancellationToken] continueWithBlock:^{
if (cancellationToken.isCancelled) {
return [BFTask cancelledTask];
}
// Do something that takes a while.
return result;
}];
}

// Somewhere else.
MYCancellationToken *cancellationToken = [[MYCancellationToken alloc] init];
[obj doSomethingComplicatedAsync:cancellationToken];

// When you get bored...
[cancellationToken cancel];
```
**Note:** The cancellation token implementation should be thread-safe.
We are likely to add some concept like this to Bolts at some point in the future.
# Installation
You can download the latest framework files from our [Releases page](https://github.com/BoltsFramework/Bolts-iOS/releases).
Expand Down

0 comments on commit e48ea14

Please sign in to comment.