fab*_*ice 6 xcode ruby-on-rails ios swift alamofire
我正在使用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)
}
Run Code Online (Sandbox Code Playgroud)
但是,我注意到.progress块只在post请求结束后才被调用,而不是多次调用以实际跟踪进度. println("ENTER .PROGRESSS")只被调用一次(最后)
我如何使.progress与Alamofire.request POST一起使用?
另外:我的参数包括base64编码的图像字符串.我正在使用后端Ruby on Rails来处理图像.这个过程需要相当长的时间.
你可以做的是首先使用ParameterEncoding枚举来生成HTTPBody数据.然后,您可以将数据拉出并将其传递给Alamofire upload方法.这是相同功能的修改版本,可在操场中编译,而是使用该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)
}
Run Code Online (Sandbox Code Playgroud)
正如@mattt最初在上面的评论中提到的,这肯定会有进展更新.
| 归档时间: |
|
| 查看次数: |
8156 次 |
| 最近记录: |