在异步功能上同时获取进度和错误操作

问题描述 投票:-1回答:1

我在Home VC中上传了Alamofire的文件。但是在类函数中上传func。所以我没有得到进展操作。我想我必须写代理方法。

有人会帮帮我吗?

我的问题在ViewController中!

在SendPhoto.class中

class SendPhoto {


    func send() {
        Alamofire.upload(
        multipartFormData: { multipartFormData in
            ...
    },
        to: "",
        headers:headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.uploadProgress {progress in
                    print(progress.fractionCompleted)

                }
                upload.responseJSON { response in

                }
            case .failure(let encodingError):
                print("File Upload Error")
                print(encodingError)
            }
    })
}}

在ViewController中

class HomepageVC: UIViewController .. {

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


    @IBAction func sendAction(_ sender: Any) {

        SendPhoto().send()

        // I want to keep this function progress and print to a label or etc. !!!
    }

}
ios swift alamofire swift4 alamofireimage
1个回答
2
投票

但是你的问题没有完成,所以假设

除了多部分请求之外,Alamofire在任何上传请求中返回UploadRequest对象

并且上传请求具有属性进度

/// The progress of fetching the response data from the server for the request.
open var progress: Progress { return dataDelegate.progress }

可用于跟踪上传的进度

编辑

您必须将带闭包的数据传递给视图控制器

这是你的方法

private func uploadAnyThing  (progress:@escaping ((Progress) -> Void) , completed:@escaping ((_ success:Bool,_ error:Error?) -> Void)) {
    Alamofire.upload(multipartFormData: { (data) in

    }, to: "test.com") { (result) in
        switch (result) {
        case .success(let request, _, _):
            request.uploadProgress(closure: { (prog) in
                progress(prog)
            })

            request.responseJSON(completionHandler: { (res) in
                completed(true,nil)

            })

        case .failure(let error) :
            completed(false,error)
            break
        }
    }
}

你可以得到它

func test () {
    uploadAnyThing(progress: { (progres) in
        print(progres.fractionCompleted)
    }) { (success, error) in

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.