Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中为多个Alamofire调用设置渐进视图_Ios_Swift_Alamofire - Fatal编程技术网

Ios 在swift中为多个Alamofire调用设置渐进视图

Ios 在swift中为多个Alamofire调用设置渐进视图,ios,swift,alamofire,Ios,Swift,Alamofire,在一个ViewController中,我有三个Alamofire API调用…我希望在三个API调用完成后将渐进式视图的进度设置为100% PS:还有,有人能教我如何在一个AlamofireAPI调用中也这么做吗 提前谢谢 使用阿拉莫菲尔的工作方法 let strURL:String = "https://xxxx" let parameters: Parameters = ["keyword": "current_timestamp"] Alamofire.reque

在一个ViewController中,我有三个Alamofire API调用…我希望在三个API调用完成后将渐进式视图的进度设置为100%

PS:还有,有人能教我如何在一个AlamofireAPI调用中也这么做吗


提前谢谢

使用阿拉莫菲尔的工作方法

    let strURL:String = "https://xxxx"
    let parameters: Parameters = ["keyword": "current_timestamp"]

    Alamofire.request(strURL, method: .post, parameters: parameters, encoding: URLEncoding.default).responseJSON { (response) in
        print(response.data!)
                    print(response.result)

                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
    }

Alamofire
提供进度块,用于提供有关api进度状态的状态,您可以使用它来显示请求或响应的进度,如下所示,用于单个api

 Alamofire.request("url", method: "httpMethod", parameters: "parameter", encoding: JSONEncoding.default, headers: "headers").downloadProgress(closure: { (progress) in

    // Progress block called frequently during the lifecycle of api.
    let fractionCompletedINpercentage = progress?.fractionCompleted * 100 

    }).validate().responseData(completionHandler: { (response) in

        if response.result.isSuccess {
            // Download success
        } else if response.result.isFailure {
            // Download failed
        }

    })
对于处理多个api,您可以对每个api使用
Bool
,并通过从所有api进度中获取完成分数的最小值或平均值来显示进度


谢谢

下载进度可在Alamofire的文档中找到

let utilityQueue = DispatchQueue.global(qos: .utility)

Alamofire.download("https://httpbin.org/image/png")
.downloadProgress(queue: utilityQueue) { progress in
    print("Download Progress: \(progress.fractionCompleted)")
}
.responseData { response in
    if let data = response.result.value {
        let image = UIImage(data: data)
    }
}

也就是说,如果API响应是gzip的,或者没有设置内容长度头,那么它可能不起作用。

回答得不错,但与他的问题无关。他要求处理单个api的进度。