I'm calling the following function in a SwiftUI View modifier in Xcode 16.1:
nonisolated function f -> CGFloat {
let semaphore = DispatchSemaphore(value: 0)
var a: CGFloat = 0
DispatchQueue.main.async {
a = ...
semaphore.signal()
}
semaphore.wait()
return a
}
The app freezes, and code in the main queue is never executed.
Right. That’s because you’re deadlocking the main thread:
-
Your
f()
function can’t return until someone signals the semaphore. -
The semaphore can’t be signalled until the
f()
function returns and the main thread has a chance to serviceDispatchQueue.main
.
I’m not sure what you’re trying to do with a semaphore but it’s almost always the wrong tool to reach for. If you can explain more about the problem you’re trying to solve, I should be able to get you heading in the right direction.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"