Skip to content

Commit

Permalink
+[BFTask taskFromExecutor:withBlock] to easily create ad-hoc tasks
Browse files Browse the repository at this point in the history
This convenience class method makes it easier to create tasks from blocks
without needing to write the code for a temporary `BFTask` or
`BFTaskCompletionSource`. Usage:

    [[BFTask taskFromExecutor:executor withBlock:id ^{
        return work_on_queue();
    }] continueWithBlock:...];

As opposed to:

    BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
    dispatch_async(queue, ^{
        [tcs setResult:work_on_queue()];
    });
    [tcs.task continueWithBlock:...];

The convenience method also handles exceptions the same way the rest of BFTasks
does so that's another plus.

Test case is included.
  • Loading branch information
ide committed May 5, 2014
1 parent f7f65de commit 8ca1cee
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Bolts/BFTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ typedef id(^BFContinuationBlock)(BFTask *task);
*/
+ (instancetype)taskWithDelay:(int)millis;

/*!
Returns a task that will be completed after the given block completes with
the specified executor.
@param executor A BFExecutor responsible for determining how the
continuation block will be run.
@param block The block to immediately schedule to run with the given executor.
@returns A task that will be completed after block has run.
If block returns a BFTask, then the task returned from
this method will not be completed until that task is completed.
*/
+ (instancetype)taskFromExecutor:(BFExecutor *)executor
withBlock:(id (^)())block;

// Properties that will be set on the task once it is completed.

/*!
Expand Down
5 changes: 5 additions & 0 deletions Bolts/BFTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ + (BFTask *)taskWithDelay:(int)millis {
return tcs.task;
}

+ (BFTask *)taskFromExecutor:(BFExecutor *)executor
withBlock:(id (^)())block {
return [[self taskWithResult:nil] continueWithExecutor:executor withBlock:block];
}

#pragma mark - Custom Setters/Getters

- (id)result {
Expand Down
12 changes: 12 additions & 0 deletions BoltsTests/TaskTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ - (void)testWaitUntilFinished {
XCTAssertEqualObjects(@"foo", task.result);
}

- (void)testTaskFromExecutor {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0L);
BFExecutor *queueExecutor = [BFExecutor executorWithDispatchQueue:queue];

BFTask *task = [BFTask taskFromExecutor:queueExecutor withBlock:^id() {
XCTAssertEqual(queue, dispatch_get_current_queue());
return @"foo";
}];
[task waitUntilFinished];
XCTAssertEqual(@"foo", task.result);
}

- (void)testExecuteImmediately {
XCTAssertTrue([NSThread isMainThread]);
BFTask *task = [BFTask taskWithResult:nil];
Expand Down

0 comments on commit 8ca1cee

Please sign in to comment.