Skip to content

Instantly share code, notes, and snippets.

@sanekgusev
Created December 5, 2017 14:55
Show Gist options
  • Save sanekgusev/56377a3029e20ef742d160725db2cd48 to your computer and use it in GitHub Desktop.
Save sanekgusev/56377a3029e20ef742d160725db2cd48 to your computer and use it in GitHub Desktop.
Nested Runloops
import Foundation
func asynchronousFunction(with completion: @escaping () -> Void) {
DispatchQueue.global().async {
Thread.sleep(forTimeInterval: 1)
DispatchQueue.main.async {
print("running callback on main thread")
completion()
}
}
}
let runloop = CFRunLoopGetCurrent()
asynchronousFunction {
// Stop the innermost invocation of `runloop`.
// Supposedly can also be called from threads other than the host
// thread of `runloop`.
CFRunLoopStop(runloop)
}
print("Starting nested runloop")
CFRunLoopRun()
print("Nested runloop stopped")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment