Alamofire请求的进度 [英] Progress of a Alamofire request

查看:67
本文介绍了Alamofire请求的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能显示

的进度

  Alamofire.request(.POST,URL,参数:parameter,编码: .JSON)
.responseJSON {响应
//做你的东西
}

我以base64字符串的形式获取图像/文档,然后将其转换为移动设备中的文件



我可以显示带有百分比的进度栏吗?



我正在使用Alamofire,Swift 2

解决方案

监视进度的方式Alamofire在 Request 上使用 progress 闭包。有关用法的更多详细信息,请参见自述文件。尽管自述文件中的示例演示了下载请求的用法,但它仍然也适用于数据请求。



一个重要的注意事项是,您并不总是很完美从服务器返回报告数据请求。原因是服务器在流传输数据之前并不总是报告准确的内容长度。如果内容长度未知,则将调用进度关闭,但 totalExpectedBytesToRead 将始终为 -1 。 / p>

在这种情况下,如果您知道所下载数据的大致大小,则只能报告准确的进度。然后,您可以将您的近似数字与 totalBytesRead 值一起使用,以计算估计的下载进度值。


Will it be possible to show progress for

Alamofire.request(.POST, URL, parameters: parameter, encoding: .JSON)
 .responseJSON { response in
 // Do your stuff
}

I get my images/documents as a base64 string then I convert it as files in mobile

Can I show a progress bar with percentage?

I am using Alamofire, Swift 2

解决方案

The way you monitor progress in Alamofire is using the progress closure on a Request. More details on usage can be found in the README. While that example in the README demonstrates usage on a download request, it still works on a data request as well.

The one important note is that you do not always get perfect reporting back from the server for a data request. The reason is that the server does not always report an accurate content length before streaming the data. If the content length is unknown, the progress closure will be called, but the totalExpectedBytesToRead will always be -1.

In this situation, you can only report accurate progress if you know the approximate size of the data being downloaded. You could then use your approximate number in conjunction with the totalBytesRead value to compute an estimated download progress value.

这篇关于Alamofire请求的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆