如何修改自定义GCD队列的优先级? [英] How to modify the priority of a custom GCD queue?
问题描述
我已经这样创建了一个GCD队列:
I've created a GCD queue like this:
dispatch_queue_t q = dispatch_queue_create("com.testcompany.myqueue", NULL);
当我将任务分派到该队列时,它比主线程上执行任务慢。
When I dispatch tasks to that queue, it is way slower than simply executing the task on the main thread.
dispatch_async(q, ^(void) {
[self performHeavyCalculationAndUpdateUI];
});
我猜测队列默认的优先级很低。
如何更改此队列的优先级?
My suspicion is that the queue has a very low priority by default. How can I change the priority of this queue? Or is there something else I must do?
推荐答案
如果你正在做UIKit的东西,在你的块中运行在辅助队列,通过以下方式将UI更新从辅助队列中调度回主队列:
If you're doing UIKit stuff, in your block that's running on the secondary queue, dispatch the UI updates back to the main queue from within the secondary queue via:
dispatch_async(q, ^(void) {
[self performHeavyCalculationAndUpdateUI];
dispatch_async(dispatch_get_main_queue(), ^{
// do my ui update
});
});
只要将实际更新UI的几行分配给主队列,不是队列优先级的问题,而只是确保UI更新发生在主队列中。
Just dispatch the few lines that actually update the UI back to the main queue and you'll see it's not a matter of queue priorities, but rather just making sure that UI updates happen in main queue.
这篇关于如何修改自定义GCD队列的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!