Skip to content

Instantly share code, notes, and snippets.

@jkubicek
Last active June 30, 2017 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkubicek/2b78ab72006497086bcba73a7c52acff to your computer and use it in GitHub Desktop.
Save jkubicek/2b78ab72006497086bcba73a7c52acff to your computer and use it in GitHub Desktop.
Dispatch Alert Queue for iOS
//: Playground - noun: a place where people can play
import Foundation
import PlaygroundSupport
class AlertQueue {
private let queue = DispatchQueue(label: "com.collectivehealth.alertqueue")
typealias Completion = () -> ()
func submit(block: @escaping () -> ()) -> Completion {
let semephore = DispatchSemaphore(value: 0)
let completion: Completion = {
semephore.signal()
}
queue.async {
DispatchQueue.main.async {
block()
}
semephore.wait()
}
return completion
}
}
let alertQueue = AlertQueue()
let ftuCompletion = alertQueue.submit {
print("FTU running on the main thread")
sleep(1)
}
let loginAlert = alertQueue.submit {
print("Presenting the login alert")
sleep(1)
}
let someOtherAlert = alertQueue.submit {
print("Presenting still another alert")
sleep(1)
}
//ftuCompletion()
//loginAlert()
//someOtherAlert()
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment