【问题标题】:Tracking progress in Alamofire Request [duplicate]跟踪 Alamofire 请求中的进度 [重复]
【发布时间】:2019-10-18 08:57:36
【问题描述】:

我想知道是否可以显示请求的进度。我将图像转换为 base64 字符串并使用参数将其发送到我的服务器。有没有办法跟踪它的进度?我想尝试类似的东西。但我无法在我的 Alamofire.request 中添加进度部分。有什么我遗漏的吗?

Alamofire.request(.POST, URL, parameters: parameter, encoding: .JSON)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
 // track progress here
 }
.responseJSON { response in
// Do your stuff
}

【问题讨论】:

  • 是的,但我知道我的数据的大概大小,我想显示进度。
  • 编辑您的问题,提供有关您的问题和您想要实现的目标的更多详细信息。正如建议的那样,这看起来像是重复的。你的意思是要更新 UI?
  • 不,我正在尝试在我的 Alamofire.request 中添加进度部分。猜猜我错过了什么。顺便说一句,我刚刚更新了我的问题。 @Soroush

标签: ios swift alamofire


【解决方案1】:

不确定,但我认为当前版本的 Alamofire 使用 downloadProgress 而不是 progress

Alamofire.request(/* ... */).downloadProgress { progress in
  progress.fractionCompleted // value between 0 and 1
}
.responseJSON { /* ... */ }

【讨论】:

  • 我试过了,但下载进度 fractionCompleted 始终为 0,totalUnitCount 为 -1。这有什么问题?
【解决方案2】:

你可以这样做

        Alamofire.upload(multipartFormData: { (multipartFormData) in





        }, with: URL, encodingCompletion: { (result) in

            switch result {

            case .success(let upload, _, _):



                upload.uploadProgress(closure: { (Progress) in



                    // Here you get the progress

                    print(Progress.fractionCompleted)




                })


                upload.responseJSON { response in






            case .failure( _ ):



            }

        })

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多