执行同步下载时显示 UIActivityIndicator
我正在下载 XML 来填充用于构建 UITableView 的数组。在我收到其他通知之前,我相信我必须完全下载该数组才能将其显示在表中(而且它只是文本并且非常小,因此它会在合理的时间内以最慢的连接下载)。最慢时大约需要 3-5 秒,因此下载时最好在状态栏中显示活动指示器。
我在做任何事情之前都会打电话给……
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
(然后在完成所有事情后将其关闭),但它只是在人眼可见的最短毫秒内弹出并退出。
关于我为什么会有这种经历有什么建议吗?
谢谢, Z@K!
I am downloading XML to populate an array used to build UITableView. Until I'm informed otherwise, I believe I have to completely download the array before I can display it in the table (also it's text only and extremely small, so it downloads in a reasonable time on the slowest possible connection). It does take about 3-5 seconds at it's slowest, so it would be nice to display the activity indicator in the status bar while it downloads.
I make the call to...
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
...before I do ANYTHING (then turn it off once I've done EVERYTHING), but it just pops and quits in about the minimum amount of milliseconds that make it visible to the human eye.
Any suggestions as to why I'm having this experience?
Thanks,
Z@K!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
同步下载在调用线程上执行,并阻塞线程直到完成,这可能是在与 UI 相同的线程上完成的。由于下载会阻塞线程直到完成,因此您要么看不到活动指示器,要么它将显示并且在下载完成之前不会移动。
您必须将同步下载放在单独的线程上或使用 NSURLConnection:initWithRequest (多线程)才能让应用程序按预期响应。
Synchronous downloads are performed on the calling thread and blocks the thread until it is complete, which is probably done on the same thread as your UI. As the download is blocking the thread till its complete you either won't see the activity indicator or it will display and not move till the download is complete.
You will have to put the synchronous download on a separate thread or use NSURLConnection:initWithRequest (which is multithreaded) to be able to have the App respond as expected.
对我来说最简单的答案是 GCD,Grand Central Dispatch。我几乎不需要修改我的代码...
我的代码是这样开始的...
*webQuery 是一个自定义对象,它从网络下载和解析 xml 数据。
**downloadAndParseXMLForTable 是一个自定义方法,它同步下载并解析 XML 文件,然后返回一个 (NSArray *) 对象以支持表视图。
下面修改后的代码显示了我为采用 GCD 所做的唯一更改,并保持 UI 响应。
就是这样!我希望这可以帮助处于困境的其他人......
干杯,
Z@K!
警告:在 WWDC 2010 上,有人提到 GCD 目前无法支持安全传输。我不记得细节了,但演讲者奎因对此非常坚定。我相信他建议的过程需要 NSOperation...
The easy answer for me was GCD, Grand Central Dispatch. I barely had to modify my code at all...
My code started as this...
*webQuery is a custom object that downloads and parses xml data from the web.
**downloadAndParseXMLForTable is a custom method that synchronously downloads and parses an XML file, then returns an (NSArray *) object to support a table view.
The modified code below, shows the ONLY changes I had to do to adopt GCD, and keep my UI responsive.
That's it! I hope this helps others in my predicament...
Cheers,
Z@K!
WARNING: At the WWDC 2010, it was mentioned that GCD cannot currently support SECURE transmissions. I don't remember the details, but the speaker, Quinn, was very adamant about it. I believe the process he suggested required NSOperation...