diff --git a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h index 3cdffb3..982ed5d 100644 --- a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h +++ b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.h @@ -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 diff --git a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m index 8ff96fb..9791ace 100644 --- a/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m +++ b/MVMCore/MVMCore/Utility/Helpers/MVMCoreDispatchUtility.m @@ -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