Add safe queue sync.
This commit is contained in:
parent
634cceff73
commit
0a3b124a51
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user