Swift 3 Alamofire请求进展

问题描述 投票:0回答:2

我有这种代码

let req = Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
    req.downloadProgress { (progress) in
        print("progess!", Float(req.progress.fractionCompleted))
    }

    req.responseJSON { response in
        if response.result.isSuccess {
            print(response.result.value!)
        }
        else {
            print(response.result.error!)
        }
    }

结果并没有给我一个进展。它只是返回1.0,就是这样,但我需要这个请求的进展。

请有人帮帮我

swift alamofire
2个回答
1
投票

使用req.progress.fractionCompleted时你犯了一个错误。改用progress.fractionCompleted

let req = Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
    req.downloadProgress { (progress) in
        print("progess!", Float(progress.fractionCompleted))
    }

    req.responseJSON { response in
        if response.result.isSuccess {
            print(response.result.value!)
        }
        else {
            print(response.result.error!)
        }
    }

0
投票

在主队列中添加进度

request?.downloadProgress(queue: .main,closure: {progress in
        self.downloadProcess.text = "Download \((progress.fractionCompleted*100))% completed"
        print(self.downloadProcess.text)
        if #available(iOS 11.0, *) {

        } else {
            // Fallback on earlier versions
        }

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