화면 S를 상상해보십시오. 사용자가 S에 도착하면 물건을보십시오. 버튼 B ... DispatchSemaphore를이 "starting busy"투기 프로세스 시나리오에서 사용하십시오.
| |
| B|
| |
| |
당신이 B 누르면
이 ..func clickedB() {
blockingSpinner = true
longCalculation()
blockingSpinner = false
showResult()
}
func longCalculation() {
// a few seconds
}
있다 (경우 계산이 일어나고있는 동안 우리는 /, 모달 스피너를보고, 사용자는 단지 기다리고 싶지는.) 사용자가 스크린 S에 도착했을 때
일반적으로, 그들은 B.
그래서를 ... 만지기 전에 몇 초에 뭔가 다른 볼
var waitor = DispatchSemaphore(value: 0) // or ???
func viewDidLoad() {
DispatchQueue.global(qos: .background).async { longCalculation() }
}
func longCalculation() {
something waitor
do the calculation
something waitor
DispatchQueue.main.async {
something waitor
}
}
func clickedB() {
// (note that ... calculation may have finished ages ago
// or we may be in the middle of it, it has a second or so remaining
// or perhaps even this is the second+ time the user has clicked B)
something waitor
if/while longCalculation is still running,
blockingSpinner = true
blockingSpinner = false
showResult()
}
나는이 시나리오에서 DispatchSemaphore
을 어떻게 사용하는지 전혀 모른다.
그들이 만든 구체적인 방법은 wait()
과 signal()
의 작업을 여기에 추가하지 않는 것 같습니다.
이 시나리오에서 DispatchSemaphore
을 사용하는 방법은 무엇입니까?
아래의 답변은 Josh가 SO 네트워크 전체에 대한 단일 답변 중 하나 여야합니다. 오크. – Fattie