Skip to content

Commit

Permalink
Merge pull request #1286 from RomanPodymov/feature/ensure_in_background
Browse files Browse the repository at this point in the history
ensureInBackground + QOS_CLASS_UNSPECIFIED
  • Loading branch information
mxcl committed Feb 28, 2022
2 parents 7b07b21 + 35b6988 commit 72daa70
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Sources/AnyPromise.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ typedef void (^PMKResolver)(id __nullable) NS_REFINED_FOR_SWIFT;
*/
- (AnyPromise * __nonnull(^ __nonnull)(dispatch_queue_t __nonnull, dispatch_block_t __nonnull))ensureOn NS_REFINED_FOR_SWIFT;

/**
The provided block is executed on the global background queue when the receiver is resolved.
@see ensure
*/
- (AnyPromise * __nonnull(^ __nonnull)(dispatch_block_t __nonnull))ensureInBackground NS_REFINED_FOR_SWIFT;

/**
Wait until the promise is resolved.
Expand Down
6 changes: 6 additions & 0 deletions Sources/AnyPromise.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ - (id)__d {
};
}

- (AnyPromise *(^)(dispatch_block_t))ensureInBackground {
return ^(dispatch_block_t block) {
return [self->d __ensureOn:dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0) execute:block];
};
}

- (id)wait {
return [d __wait];
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/after.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
AnyPromise *PMKAfter(NSTimeInterval duration) {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC));
dispatch_after(time, dispatch_get_global_queue(0, 0), ^{
dispatch_after(time, dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
resolve(@(duration));
});
}];
Expand Down
2 changes: 1 addition & 1 deletion Sources/dispatch_promise.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
}

AnyPromise *dispatch_promise(id block) {
return dispatch_promise_on(dispatch_get_global_queue(0, 0), block);
return dispatch_promise_on(dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), block);
}
10 changes: 8 additions & 2 deletions Tests/CoreObjC/AnyPromiseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

static inline AnyPromise *fulfillLater() {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{
resolve(@1);
});
}];
Expand Down Expand Up @@ -797,7 +797,13 @@ - (void)testInBackground {

- (void)testEnsureOn {
id ex = [self expectationWithDescription:@""];
PMKAfter(0.1).ensureOn(dispatch_get_global_queue(0, 0), ^{ [ex fulfill]; });
PMKAfter(0.1).ensureOn(dispatch_get_global_queue(QOS_CLASS_UNSPECIFIED, 0), ^{ [ex fulfill]; });
[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)testEnsureInBackground {
id ex = [self expectationWithDescription:@""];
PMKAfter(0.1).ensureInBackground(^{ [ex fulfill]; });
[self waitForExpectationsWithTimeout:1 handler:nil];
}

Expand Down

0 comments on commit 72daa70

Please sign in to comment.