iOS多线程 - NSURLSession和UI更新 [英] iOS Multithreading - NSURLSession and UI updates

查看:359
本文介绍了iOS多线程 - NSURLSession和UI更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iOS中的多线程有一个普遍的疑问:



在我非常简单的测试应用中,我使用NSURLSession从服务器下载一些小图像并将它们呈现在表格视图。在NSURLSession的回调中,在检索图像之后,我像这样调用tableview.reloadData():

  var session = NSURLSession .sharedSession()。dataTaskWithURL(NSURL(url)){(数据,响应,错误) - >无效
/ *此处处理图像* /
self.tableView.reloadData()
} session.resume()

图像下载几乎是即时的,但表视图需要10-20秒才能更新!要清楚,更新代表不是甚至将称为 10-20秒。但是,如果我在主线程上放置reloadData(),它会快速更新。像这样:

  var session = NSURLSession.sharedSession()。dataTaskWithURL(NSURL(url)){(数据,响应,错误) - >空白
/ *此处处理图像* /
dispatch_async(dispatch_get_main_queue()){
self.tableView.reloadData()
}
} session.resume()

所以,我的问题解决了,我可以再次入睡。但有人可以向我解释为什么会这样吗?我知道UI更新应该在主线程上执行 - 那么为什么第一个案例实际上仍然有用,但是要永远这样做呢?应用程序中没有其他任何内容,所以我甚至无法开始考虑什么可以阻止重新加载整整20秒。它是在等待NSURLSession首先完全关闭,还是沿着这些线路?寻找大素数?挖掘比特币?



我对多线程的了解是有限的,所以对这种情况的任何见解都会对将来的参考非常有帮助。

解决方案

我们不会在除main之外的任何线程中更新UI,因为它会导致死锁。这里可能发生的事情是,会有10-20秒的死锁,之后会发生死锁。



这并不是说在另一个线程上更新UI总是行不通的。您所经历的是众多成果之一。我们无法真正告诉Cocoa下面发生了什么,所以我们真的不知道。



只需要在主线程上更新你的UI,以避免像这样的奇怪行为。 / p>

https://chritto.wordpress.com/2012/12/20/updating-the-ui-from-another-thread/


I have a general question about multithreading in iOS:

In my very simple test app, I use NSURLSession to download some small images from the server and present them in a table view. Within the callback of the NSURLSession, after retrieving the images, I call tableview.reloadData() like so:

var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url)) {(data, response, error) -> Void in
    /* Process images here */
    self.tableView.reloadData()
} session.resume()

The images download almost instantaneously, but it takes 10-20 seconds for the table view to update! To be clear, the update delegates aren't even getting called for 10-20 secs. However, if I place reloadData() on the main thread it updates quickly. Like so:

var session = NSURLSession.sharedSession().dataTaskWithURL(NSURL(url)) {(data, response, error) -> Void in
    /* Process images here */
    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
} session.resume()

So, my problem is solved and I can sleep easy again. But can somebody please explain to me why this is the case? I understand that UI updates should be performed on the main thread - so why does the first case actually still work, yet take FOREVER to do it? There is nothing else going on in the app, so I can't even begin to think what could be holding up the reload for an entire 20 seconds. Is it waiting for the NSURLSession to fully close first, or something along those lines? Finding large prime numbers? Mining Bitcoin?

My knowledge of multithreading is limited, so any insight about this situation would be extremely helpful for future reference.

解决方案

We don't update UI in any thread other than main because it causes deadlocks. What probably happens here is that there's a deadlock for 10-20 seconds and after that time something releases the deadlock.

It's not that updating UI on another thread will always not work. What you experienced is one of many outcomes. We can't really tell what's happening underneath Cocoa so we can't really know.

Just always update your UI on the main thread to avoid strange behavior like that.

https://chritto.wordpress.com/2012/12/20/updating-the-ui-from-another-thread/

这篇关于iOS多线程 - NSURLSession和UI更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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