Progress bar while downloading

元气小坏坏 提交于 2019-12-06 01:35:06

The problem is that dataWithContentsOfURL: is a blocking call. This means that it will block the thread that it is running on.

You've got a couple of options to fix this, and the better one is probably to use an NSURLConnection.

With an NSURLConnection you can perform the download request asynchronously, which will prevent it from blocking the main thread.

You must use the NSURLConnectionDelegate methods to be informed of the progress of the download, save its data, and to be informed of success or failure.

Please read the documentation for the NSURL Loading System.

An alternative to using NSURLConnection is to wrap your current code with some calls to GCD using dispatch queues. This will prevent the call from blocking your UI, but it will not allow you to determine the progress - for that you will still need to use NSURLConnection.

You should really have a look at the ASIHTTPRequest and especially this section

It provides callbacks for tracking your downloads, async and sync connections, queues, caching and lots of good stuff.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!