Libdispatch and REPL

Doesn't look like libdispatch interacts well with the REPL.

1> import Foundation
2> DispatchQueue.global(qos: .userInitiated).async { print("what") } 

never prints. Anything that can be done to fix?

Try this:

3> RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))

The Swift REPL is built around LLDB, so the process running Swift is suspended while you’re entering commands in the REPL. Thus, things that you schedule to run asynchronously don’t run. To get them to run you have to let the process run for a while. Doing that with a run loop (rather than, say, Thread.sleep(until:)) is good because it’ll let run loop based callbacks run as well.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes
Terms of Service

Privacy Policy

Cookie Policy