在CoreML VNCoreMLRequest之前触发时未显示ActivityIndi​​cator

时间:2018-07-27 22:59:41

标签: ios swift grand-central-dispatch uiactivityindicatorview coreml

我正在使用快速的ActivityIndi​​cator:https://github.com/ninjaprox/NVActivityIndicatorView

func runMLModel(image: CIImage, completion: @escaping ([MLMultiArray]) -> Void){
     let request = VNCoreMLRequest(model: myModel) { (request, error) in
            guard let results = request.results as? [VNCoreMLFeatureValueObservation] else {
                fatalError("Model failed to process image")
            }
            //Do work here get mlData var
            completion(mlData)
        }

        let handler = VNImageRequestHandler(ciImage: image)

        do {
            try handler.perform([request])
        }catch {
            print(error)
        }
}

func buttonPressedToDoMLWork(){

     self.startAnimating(CGSize(width: 200, height: 200), message: "loading", type: NVActivityIndicatorType.audioEqualizer)  
     DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
        NVActivityIndicatorPresenter.sharedInstance.setMessage("Authenticating...")

    }
    // Activity Indicator should be showing up here
    // but nothing is displayed
    runMLModel(image: myImage, completion: { (mlData : [MLMultiArray]) in

            print("got data")

            //indicator only shows up NOW.. after printing
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                self.stopAnimating(nil)

            }        
        })
    }
}

CoreML模块中的代码似乎覆盖了在主DispatchQueue上启动NVAcitivityIndi​​catorPresenter的重要性。

是否要在触发MLRequest之前显示此损坏指示?

1 个答案:

答案 0 :(得分:0)

将rumMlmodel设置为延迟功能,不要在异步任务中添加活动代码。之后,它将显示您的活动。延迟应该在0.4-0.6

左右