Last active
June 30, 2017 18:29
-
-
Save jkubicek/2b78ab72006497086bcba73a7c52acff to your computer and use it in GitHub Desktop.
Dispatch Alert Queue for iOS
This file contains hidden or 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
//: 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