Alamofire请求的进度

编程入门 行业动态 更新时间:2024-10-27 01:28:47
本文介绍了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请求的进度

本文发布于:2023-11-26 04:44:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!