diff --git a/Sources/AnyPromise.h b/Sources/AnyPromise.h index 75352d55c..34b5252d4 100644 --- a/Sources/AnyPromise.h +++ b/Sources/AnyPromise.h @@ -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. diff --git a/Sources/AnyPromise.m b/Sources/AnyPromise.m index 3725beacd..5b2626242 100644 --- a/Sources/AnyPromise.m +++ b/Sources/AnyPromise.m @@ -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]; } diff --git a/Sources/after.m b/Sources/after.m index 25f9966fc..d98fb381b 100644 --- a/Sources/after.m +++ b/Sources/after.m @@ -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)); }); }]; diff --git a/Sources/dispatch_promise.m b/Sources/dispatch_promise.m index ecb89f711..f4f10e38c 100644 --- a/Sources/dispatch_promise.m +++ b/Sources/dispatch_promise.m @@ -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); } diff --git a/Tests/CoreObjC/AnyPromiseTests.m b/Tests/CoreObjC/AnyPromiseTests.m index d0726cf78..d2498b496 100644 --- a/Tests/CoreObjC/AnyPromiseTests.m +++ b/Tests/CoreObjC/AnyPromiseTests.m @@ -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); }); }]; @@ -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]; }