NSURLConnection是否阻止主/ UI线程

ric*_*son 6 iphone objective-c nsurlconnection ipad

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

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

**编辑**

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

就个人而言,我认为NSURLConnection被抛入到UITableView滚动阻止它的事件循环中.

Gab*_*abe 22

阅读NSDefaultRunLoopMode与NSRunLoopCommonModes,以便很好地解释为什么所有下载委托通知都排队,但在使用主线程更改时滚动下载:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self];
Run Code Online (Sandbox Code Playgroud)

对此:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                              delegate:self
                                                      startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
                      forMode:NSRunLoopCommonModes];
[connection start];
Run Code Online (Sandbox Code Playgroud)