在这个 "starts busy" 推测处理场景中使用 DispatchSemaphore

Use DispatchSemaphore in this "starts busy" speculative processing scenario

想象一个屏幕 S。用户到达 S,看东西。有一个按钮 B ...

|    |
|   B|
|    |
|    |

当你按 B ..

func clickedB() {

   blockingSpinner = true
   longCalculation()
   blockingSpinner = false
   showResult()
}

func longCalculation() {

   // a few seconds
}

(我们希望用户等待,看到模态微调器,if/while 计算正在进行。)

通常,当用户到达屏幕 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

您希望一次只能处理一件事,因此您的信号量值应为 1。鉴于此,您也可以轻松地使用 NSLock。但这里是使用信号量的概要。

当你开始计算时,wait()(无限期地)在信号量上。正如您所建议的,您可以利用视图控制器生命周期的固有顺序来了解这实际上不会阻塞。每当你完成时,`signal().显然这一切都应该在后台完成,这样主线程才不会被阻塞。

当您处理按钮点击时,通过 "waiting" 测试信号量并立即超时,如果得到 .timedOut 结果则显示微调器。我们可以在主线程上进行,因为无论信号量可用还是超时,都不会有实际的等待时间。注意这里不需要信号量:如果等待超时,信号量会自动重新递增。如果等待成功,则工作完成——直接继续显示结果。

如果作业未完成,则显示微调器;现在(无限期地)在后台等待。当等待结束时——因为信号量已发出信号——跳回主线程,关闭微调器,并显示结果。

此时,信号量的计数为 0,因此如果您需要再次通过此代码路径,则必须向信号量发出信号。

基本上,填写您制作的代码草图:

class WhateverViewController : UIViewController
{
    private let semaphore = DispatchSemaphore(value: 1)

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.performLongCalculation()
    }

    private func performLongCalculation()
    {
        DispatchQueue.global(qos: .background).async {
            self.semaphore.wait()
            // Synchronous processing...
            self.semaphore.signal()
        }
    }

    private func buttonTapped()
    {
        if self.semaphore.isBusy {
            self.waitForResult()
        }
        else {
            self.showResult()
        }
    }

    private func buttonTappedAlternative()
    {
        // Show the spinner unconditionally, if you assume that the
        // calculation isn't already done.
        self.waitForResult()
    }

    private func waitForResult()
    {
        self.showSpinner()
        DispatchQueue.global(qos: .userInitiated).async {
            self.semaphore.wait()
            DispatchQueue.main.async {
                self.dismissSpinner()
                self.showResult()
            }
        }
    }

    private func showResult()
    {
        // Put stuff on screen
        self.semaphore.signal()
    }
}

其中 buttonTappedDispatchSemaphore

上使用便利
extension DispatchSemaphore
{
    var isBusy : Bool { return self.wait(timeout: .now()) == .timedOut }
}

如果您愿意,您可以颠倒这个逻辑:isIdle 只是 self.wait(timeout: .now()) == .success