Swift 4:从DispatchQueue.main(范围)访问变量

时间:2017-12-07 13:09:29

标签: swift scope dispatch-queue

我有一个CoreML图像分类任务,它采用"直播流"来自iOS设备的[视频]摄像头,并在后台进行。一旦识别出对象,并且发生了其他应用程序逻辑,我想用一些数据更新UI标签。

有人可以解释DispatchQueue.main.asyc(execute: { })的标注如何能够访问我一直在使用的变量吗?我认为这本质上是一个范围问题?

我目前使用的代码:

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

    processCameraBuffer(sampleBuffer: sampleBuffer)

}

func processCameraBuffer(sampleBuffer: CMSampleBuffer) {

    let coreMLModel = Inceptionv3()

    if let model = try? VNCoreMLModel(for: coreMLModel.model) {
        let request = VNCoreMLRequest(model: model, completionHandler: { (request, error) in
            if let results = request.results as? [VNClassificationObservation] {

                var counter = 0
                var otherVar = 0

                for item in results[0...9] {

                    if item.identifier.contains("something") {
                        print("some app logic goes on here")
                        otherVar += 10 - counter
                    }
                    counter += 1

                }
                switch otherVar {
                case _ where otherVar >= 10:
                    DispatchQueue.main.async(execute: {
                        let displayVarFormatted = String(format: "%.2f", otherVar / 65 * 100)
                        self.labelPrediction.text = "\(counter): \(displayVarFormatted)%"
                    })
                default:
                    DispatchQueue.main.async(execute: {
                        self.labelPrediction.text = "No result!"
                    })
                }
            }
        })

            if let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
                let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
                do {
                    try handler.perform([request])
                } catch {
                    print(error.localizedDescription)
                }
            }
    }
}

它导致问题的switch语句中的self.labelPrediction.text = ""行。此var目前始终为0。

1 个答案:

答案 0 :(得分:0)

这不是DispatchQueue的问题。从processCameraBuffer(sampleBuffer:)开始,您的代码会在获得结果之前更新您的用户界面。

要解决此问题,您需要使用escaping closure。你的功能应该是这样的。

func processCameraBuffer(sampleBuffer: CMSampleBuffer, completion: @escaping (Int, String) -> Void) {
    // 2.
    let request = VNCoreMLRequest(model: model, completionHandler: { (request, error) in

      DispatchQueue.main.async(execute: {
          // 3.
          let displayVarFormatted = String(format: "%.2f", otherVar / 65 * 100)
          completion(counter, displayVarFormatted)
      })
    }
}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 
    // 1.
    processCameraBuffer(sampleBuffer) { counter, displayVarFormatted in
        /*
         This Closure will be executed from 
         completion(counter, displayVarFormatted)
        */
        // 4.
        self.labelPrediction.text = "\(counter): \(displayVarFormatted)%"
    }
}

变量范围不是问题。您需要处理异步任务。

  1. 捕获发生。
  2. 调用processCameraBuffer并执行VNCoreMLRequest
  3. 您将通过processCameraBuffer获取数据并执行completion()的完成功能块。
  4. 更新标签。