Created
December 5, 2017 14:55
-
-
Save sanekgusev/56377a3029e20ef742d160725db2cd48 to your computer and use it in GitHub Desktop.
Nested Runloops
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
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