-
-
Save ricardopereira/6ace4becfd0d07a4c5d6a3da936ae5eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class SafeSyncQueue { | |
struct QueueIdentity { | |
let label: String | |
} | |
let queue: DispatchQueue | |
fileprivate let queueKey: DispatchSpecificKey<QueueIdentity> | |
init(label: String) { | |
self.queue = DispatchQueue(label: "com.khanlou.\(label).SafeSyncQueue") | |
self.queueKey = DispatchSpecificKey<QueueIdentity>() | |
self.queue.setSpecific(key: queueKey, value: QueueIdentity(label: queue.label)) | |
} | |
fileprivate var currentQueueIdentity: QueueIdentity? { | |
return DispatchQueue.getSpecific(key: queueKey) | |
} | |
func sync(execute: ()->()) { | |
if currentQueueIdentity?.label == queue.label { | |
execute() | |
} else { | |
queue.sync(execute: execute) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment