ios dispatch_get_global_queue 与 dispatch_get_main_queue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12693197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dispatch_get_global_queue vs dispatch_get_main_queue
提问by tranvutuan
Starting to learn about core data and dispatch_async. There is a block of code to get url of image from set of data and set it to model of core data like below
开始学习核心数据和 dispatch_async。有一段代码可以从数据集中获取图像的 url 并将其设置为核心数据模型,如下所示
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *urlString = [[[photoDictionary valueForKey:@"images"] objectAtIndex:0] valueForKey:@"url"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
dispatch_async(dispatch_get_main_queue(), ^{
[photoModel setValue:imageData forKey:@"photoImageData"];
Can somebody explain to me why we use dispatch_get_global_queue
for the outer dispatch_async and dispatch_get_main_queue
for inner dispatch_async.
有人可以向我解释为什么我们使用dispatch_get_global_queue
外部 dispatch_async 和dispatch_get_main_queue
内部 dispatch_async。
回答by Rob
The dispatch_get_global_queue
gets you a background queue upon which you can dispatch background tasks that are run asynchronously (i.e. won't block your user interface). And if you end up submitting multiple blocks to the global queues, these jobs can operate concurrently. If you have multiple blocks of code that you want to submit to a background queue that you must have run sequentially in the background (not often needed), you could create your own serial background queue and dispatch to that, but if concurrent background operations are acceptable, then availing yourself of dispatch_get_global_queue
is convenient/efficient.
这为dispatch_get_global_queue
您提供了一个后台队列,您可以在该队列上调度异步运行的后台任务(即不会阻塞您的用户界面)。如果您最终将多个块提交到全局队列,这些作业可以并发运行。如果您有多个代码块要提交到必须在后台按顺序运行的后台队列(不经常需要),您可以创建自己的串行后台队列并分派到该队列,但如果并发后台操作是可以接受,然后利用自己dispatch_get_global_queue
是方便/高效的。
Be aware, though, that you're not allowed to perform user interface updates in the background queue, so the dispatch_async
to the dispatch_get_main_queue
lets that background queue dispatch the user interface updates back to the main queue, once the main queue is available.
但是请注意,不允许在后台队列中执行用户界面更新,因此一旦主队列可用dispatch_async
,dispatch_get_main_queue
就让后台队列将用户界面更新分派回主队列。
This is a very common programming pattern: Submit something to run in the background and when it needs to perform user updates, dispatch the update back to the main queue.
这是一个非常常见的编程模式:提交一些东西在后台运行,当它需要执行用户更新时,将更新分派回主队列。
For more information, refer to the Concurrency Programming Guide.
有关更多信息,请参阅并发编程指南。
回答by ageektrapped
The dispatch_get_main_queue
should be used anytime you want to manipulate UI elements. This has to do with thread affinity, a common model for UI frameworks. Thread affinity means you can only manipulate the object on the thread on which that object was created. For UI classes in Cocoa Touch, that's the main thread. This is a typical idiom for UI frameworks on all platforms that matter.
本dispatch_get_main_queue
应该是要操纵UI元素使用的任何时候。这与线程关联有关,这是 UI 框架的通用模型。线程关联意味着您只能在创建该对象的线程上操作该对象。对于 Cocoa Touch 中的 UI 类,这是主线程。这是所有重要平台上 UI 框架的典型习惯用法。
So dispatch_get_main_queue
gets the queue associated with the main thread. Not doing this causes weird stuff to happen when your UI is updated on a different thread. I typically see long pauses where the UI freezes.
所以dispatch_get_main_queue
获取与主线程关联的队列。当您的 UI 在不同线程上更新时,不这样做会导致奇怪的事情发生。我通常会看到 UI 冻结的长时间停顿。
dispatch_get_global_queue
gets any old queue of the given priority level associated with your app. Perfect for network calls or, as in your case, working with Core Data.
dispatch_get_global_queue
获取与您的应用程序关联的给定优先级的任何旧队列。非常适合网络呼叫,或者像您一样使用 Core Data。
回答by geraldWilliam
Global queue gives you a queue other than the main que but saves you the trouble of actually creating your own queue. Use get_main_queue when you need your code to work on the main queue (where all of your UI work needs to happen)
全局队列为您提供了一个不同于主队列的队列,但为您省去了实际创建自己的队列的麻烦。当你需要你的代码在主队列上工作时使用 get_main_queue(你所有的 UI 工作都需要在这里发生)
回答by Ankit garg
**dispatch_get_main_queue** :- Perform UI updates on this queue
dispatch_async(dispatch_get_main_queue(), ^{
self.label.text=@"Hello";
});
**dispatch_get_main_queue**:- Perform background tasks like downloading content
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (int i=0; i<100000;enter code here i++) {
NSLog(@"HIGH 1 %d", i);
}
});