diff --git a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h index c49445e..9ee0da1 100644 --- a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h +++ b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h @@ -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 diff --git a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m index 9791ace..6dce1f2 100644 --- a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m +++ b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m @@ -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);