我正在使用 Alamofire 执行 POST 请求。
由于此 POST 请求可能需要一段时间,并且我想跟踪进度并将其显示为 ProgressView。
Alamofire.request(.POST, ApiLink.create_post, parameters: parameters, encoding: .JSON)
.progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) -> Void in
println("ENTER .PROGRESSS")
println("\(totalBytesRead) of \(totalBytesExpectedToRead)")
self.progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true)
}
.responseJSON { (_, _, mydata, _) in
println(mydata)
}
然而,我注意到.进度块仅在发布请求结束后才被调用,而不是多次被调用来实际跟踪进度。println("输入.进度")仅被调用一次(最后)
如何使 .progress 与 Alamofire.request POST 一起使用?
另外:我的参数包括一个 base64 编码的图像字符串。我正在使用后端 Ruby on Rails 来处理图像。这个过程需要相当长的时间。
你可以做的是首先使用ParameterEncoding
枚举来生成 HTTPBody 数据。然后您可以提取该数据并将其传递给 Alamofireupload
方法。这是同一函数的修改版本,它在游乐场中编译,而是使用upload
功能。
struct ApiLink {
static let create_post = "/my/path/for/create/post"
}
let parameters: [String: AnyObject] = ["key": "value"] // Make sure this has your image as well
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: ApiLink.create_post)!)
mutableURLRequest.HTTPMethod = Method.POST.rawValue
let encodedURLRequest = ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
let data = encodedURLRequest.HTTPBody!
let progressView = UIProgressView()
Alamofire.upload(mutableURLRequest, data)
.progress { _, totalBytesRead, totalBytesExpectedToRead in
println("ENTER .PROGRESSS")
println("\(totalBytesRead) of \(totalBytesExpectedToRead)")
progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true)
}
.responseJSON { _, _, mydata, _ in
println(mydata)
}
正如 @mattt 最初在上面的评论中提到的那样,这肯定会有进展更新。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)