在下载NSData dataWithContentsOfURL时实现UIActivityIndi​​catorView [英] implementing UIActivityIndicatorView while NSData dataWithContentsOfURL is downloading

查看:121
本文介绍了在下载NSData dataWithContentsOfURL时实现UIActivityIndi​​catorView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSData dataWithContentsOfURL:url下载mp3.这需要一段时间,而文件正在下载,应用程序挂起.我想处理得很好,理想情况下想显示下载进度,但找不到用于此目的的方法.

I am downloading an mp3 using NSData dataWithContentsOfURL:url. This takes a while and while the file is downloading the application hangs. I want to handle well and ideal would like to show the download progress but can't find methods for this.

它位于UIViewController中,我已经进行了首次尝试,方法是放入UIActivityIndi​​catorView并在开始下载之前将其旋转,然后在没有任何显示后停止旋转.

It is in a UIViewController and I have made a first attempt by putting in a UIActivityIndicatorView and start it spinning before I start the download, then stop it spinning after but nothing appears.

所以我的问题确实是,有人可以告诉我解决此问题的最佳方法是什么吗?非常感谢

So my question really is please could someone tell me what the best way to handle this is? Thanks so much

推荐答案

什么都不会出现,因为您的主线程无法进行下载,并且主线程是进行UI更新的地方.

Nothing will appear because your main thread is blocked doing the download, and the main thread is where UI updates occur.

您应该使用 NSUrlConnection 异步下载并实现委托方法来启动/停止微调器.

You should use NSUrlConnection to download asynchronously and implement the delegate methods to start/stop your spinner.

或者,如果要坚持使用NSDatadataWithContentsOfURL:url,则应在单独的线程上执行此操作,并在调用它之前和之后更新主线程上的微调器.

Alternatively if you want to stick with NSData's dataWithContentsOfURL:url you should do this on a separate thread and update the spinner on the main thread before and after you call it.

这篇关于在下载NSData dataWithContentsOfURL时实现UIActivityIndi​​catorView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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