Add dispatch async on queue.

This commit is contained in:
Hedden, Kyle Matthew 2019-09-12 13:28:51 -04:00 committed by Pan, Xinlei (Ryan)
parent e59e77d163
commit d7fa319b05
2 changed files with 11 additions and 0 deletions

View File

@ -19,6 +19,7 @@
+ (void)performSyncBlockInBackground:(nonnull void (^)(void))block;
/// Ensures the block is peformed on the same *labeled* dispatch queue. The queue *must* be previously initialized with a unique label.
+ (void)performBlock:(nonnull void (^)(void))block onQueue:(dispatch_queue_t)queue;
+ (void)performSyncBlock:(nonnull void (^)(void))block onQueue:(nonnull dispatch_queue_t)queue;
@end

View File

@ -42,6 +42,16 @@
}
}
+ (void)performBlock:(nonnull void (^)(void))block onQueue:(dispatch_queue_t)queue {
assert(dispatch_queue_get_label(queue) != NULL); // The queue being queried MUST have a label.
const char *currQueueLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
if (!currQueueLabel || strcmp(dispatch_queue_get_label(queue), currQueueLabel) != 0) {
dispatch_async(queue, block);
} else {
block();
}
}
+ (void)performSyncBlock:(nonnull void (^)(void))block onQueue:(dispatch_queue_t)queue {
assert(dispatch_queue_get_label(queue) != NULL); // The queue being queried MUST have a label.
const char *currQueueLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);