Add safe queue sync.

This commit is contained in:
Hedden, Kyle Matthew 2019-01-25 13:22:32 -05:00
parent 634cceff73
commit 0a3b124a51
2 changed files with 13 additions and 0 deletions

View File

@ -18,4 +18,7 @@
+ (void)performBlockInBackground:(nonnull void (^)(void))block;
+ (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)performSyncBlock:(nonnull void (^)(void))block onQueue:(dispatch_queue_t)queue;
@end

View File

@ -42,4 +42,14 @@
}
}
+ (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);
if (!currQueueLabel || strcmp(dispatch_queue_get_label(queue), currQueueLabel) != 0) {
dispatch_sync(queue, block);
} else {
block();
}
}
@end