无法弄清楚如何管理MBProgressHUD和调度

时间:2017-07-05 15:19:32

标签: ios swift alamofire dispatch mbprogresshud

我正在实施iOS应用,并在将照片上传到服务器时尝试执行以下操作:

  • 使用Alamofire
  • 开始上传过程
  • 使用MBProgressHUD显示进度指示器
  • 上传完成后,隐藏进度指示器
  • 根据服务器响应,显示消息(成功或失败) 然后关闭视图

我无法实现这一点,尤其是在显示响应消息2秒后解除视图。这是我的代码。 你好吗? 感谢。

class ShareViewController: UIViewController {
    var progressHUD: MBProgressHUD! = nil

    func sendToServer(image: UIImage, imageName: String) {

        Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(jpgImageData!, withName: "photos",fileName: fname, mimeType: mime)
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        },
                         to:url!)
        { (result) in
            switch result {
            case .success(let upload, _, _):         
                upload.uploadProgress(closure: { (progress) in    
                    DispatchQueue.main.async(execute: {
                        self.progressHUD.show(animated: true)
                        self.progressHUD.progress = (Float(progress.fractionCompleted))
                        self.progressHUD.hide(animated: true, afterDelay: 3)
                    })

                })
                upload.responseJSON { response in
                    if response.response?.statusCode == 200{
                        if let result = response.result.value {
                            var message = String()
                            message = "200, OK"

                            self.progressHUD.label.text = message
                            self.progressHUD.show(animated: true)
                            self.progressHUD.hide(animated: true, afterDelay: 2)

                            DispatchQueue.main.async(execute: {
                                self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
                            })
                        }
                    }
                    else{
                        var message = String()
                        message = "Not 200, NOK"

                        self.progressHUD.label.text = message
                        self.progressHUD.show(animated: true)
                        self.progressHUD.hide(animated: true, afterDelay: 2)

                        DispatchQueue.main.async(execute: {
                            self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
                        })
                    }
                }

            case .failure(let encodingError):
                print(encodingError)
                var message = String()
                message = "Failure, NOK"

                self.progressHUD.label.text = message
                self.progressHUD.show(animated: true)
                self.progressHUD.hide(animated: true, afterDelay: 2)

                DispatchQueue.main.async(execute: {
                    self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
                })
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了一个效果很好的解决方案。 解散视图的最后一个DispatchQueue不会在2秒后立即执行。这是代码:

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                                self.extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
                            }

我希望有所帮助。