xcode 带有进度的 Alamofire POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28267796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alamofire POST request with progress
提问by fabdarice
I'm using Alamofire to do a POST request. As this POST request can take a while and I want to keep track of the progress and display it as a ProgressView.
我正在使用 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)
}
However, I've noticed that the .progress blockonly get called after the post request has ended instead of getting called multiple times to actually keep track of the progress. println("ENTER .PROGRESSS")gets called only once (at the end)
但是,我注意到.progress 块仅在 post 请求结束后才被调用,而不是被多次调用以实际跟踪进度。 println("ENTER .PROGRESSS")只被调用一次(最后)
How can I make .progress works with Alamofire.request POST ?
如何使 .progress 与 Alamofire.request POST 一起使用?
Also : My parameters include a base64 encoded image string. I'm using a back-end Ruby on Rails to process the image. It's that process that is taking quite some time.
另外:我的参数包括一个 base64 编码的图像字符串。我正在使用后端 Ruby on Rails 来处理图像。这个过程需要相当长的时间。
采纳答案by cnoon
What you can do instead is first use the ParameterEncoding
enum to generate the HTTPBody data. Then you can pull that data out and pass it off to the Alamofire upload
method. Here's a modified version of your same function that compiles in a playground and instead uses the upload
function.
您可以做的是首先使用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)
}
This will certainly have progress updates as @mattt originally mentioned in his comment above.
正如@mattt 在上面的评论中最初提到的那样,这肯定会有进度更新。
回答by Basem Saadawy
As @cnoonsaid you can use upload method to track progress with some modifications. Here is what exactly worked with me:
正如@cnoon所说,您可以使用上传方法通过一些修改来跟踪进度。以下是对我有用的内容:
let jsonData = NSJSONSerialization.dataWithJSONObject(jsonObject, options: .PrettyPrinted)
Alamofire.upload(.POST, "API URL String", headers: ["Content-Type": "application/json"], data: jsonData)
.validate()
.responseJSON(completionHandler: { (response: Response<AnyObject, NSError>) in
//Completion handler code
})
.progress({ (bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) in
//Progress handler code
})
Notethat you must set the "Content-Type"http header field value to "application/json"if the data is formatted as json to be decoded correctly at the backend.
请注意,如果数据格式为 json 以在后端正确解码,则必须将“Content-Type”http 标头字段值设置为“application/json”。