NSURLConnection是否阻止主

NSURLConnection是否阻止主

本文介绍了NSURLConnection是否阻止主/ UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在桌面视图单元格中下载图像,因为它们滚动到屏幕上。出于UX原因,我开始在 - (UITableViewCell *)tableView中下载图像:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 。我不要等到表视图完成滚动。设置表格视图单元格后,我开始下载我还没有的图像。但是,在表视图停止移动之前,它们似乎没有完全下载。一旦它停止移动,图像几乎立即下载。

I am downloading images in table view cells as they scroll onto the screen. For UX reasons, I start downloading the images in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. I do not wait until the table view is done scrolling. When the table view cell is set, I start downloading images that I do not have already. However, they do not seem to fully download until the table view stops moving. As soon as it stops moving, the images almost download instantly.

无论如何使用NSURLConnection,它没有被主UI线程阻止?或者,有一种方法可以在滚动表格视图时非常快速地下载这些图像。

Is there anyway to use NSURLConnection where it is not blocked by the main UI thread? Or, is there a way where these images will download very quickly while the table view is being scrolled.

**编辑**

为了证明NSURLConnection较慢,我使用NSThread在另一个线程中分离新的选择器。然后我下载数据并回调主线程,在那里我创建一个UIImage并在表视图中显示它。这个方法工作得更快。

To prove that NSURLConnection is slower I used NSThread to detach a new selector in a different thread. I then download the data and call back to the main thread where I create a UIImage and show it in the table view. This method works MUCH faster.

就我个人而言,我认为NSURLConnection被抛入事件循环,UITableView滚动阻止了它。

Personally, I think that NSURLConnection is getting thrown into the event loop where the UITableView scrolling is blocking it.

推荐答案

阅读一个很好的解释为什么所有下载委托通知都排队,但在使用主线程更改时滚动下载:

Read NSDefaultRunLoopMode vs NSRunLoopCommonModes for a good explanation of why all the download delegate notifications are queued up, but to download while scrolling when using the main thread change from this:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];

到此:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self
                                                      startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
                      forMode:NSRunLoopCommonModes];
[connection start];

这篇关于NSURLConnection是否阻止主/ UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:42