Skip to content

Commit

Permalink
Merge pull request #21 from ide/master
Browse files Browse the repository at this point in the history
+[BFTask taskFromExecutor:withBlock] to easily create ad-hoc tasks
  • Loading branch information
bklimt committed May 30, 2014
2 parents 452a6ee + 8ca1cee commit 3af58ae
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 3af58ae

Please sign in to comment.