Does dispatch_async(dispatch_get_main_queue(), ^{…}); wait until done?

后端 未结 8 1662
一整个雨季
一整个雨季 2020-11-28 20:45

I have a scenario in my app, where I want to do some time consuming task which consists of some data processing as well as UI update, in a method. My method looks like this,

相关标签:
8条回答
  • 2020-11-28 21:14
    dispatch_queue_t queue = dispatch_queue_create("com.example.MyQueue", NULL);
    dispatch_async(queue, ^{
      // Do some computation here.
    
      // Update UI after computation.
      dispatch_async(dispatch_get_main_queue(), ^{
        // Update the UI on the main thread.
      });
    });
    
    0 讨论(0)
  • 2020-11-28 21:15

    If you want to run a single independent queued operation and you’re not concerned with other concurrent operations, you can use the global concurrent queue:

    dispatch_queue_t globalConcurrentQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    

    This will return a concurrent queue with the given priority as outlined in the documentation:

    DISPATCH_QUEUE_PRIORITY_HIGH Items dispatched to the queue will run at high priority, i.e. the queue will be scheduled for execution before any default priority or low priority queue.

    DISPATCH_QUEUE_PRIORITY_DEFAULT Items dispatched to the queue will run at the default priority, i.e. the queue will be scheduled for execution after all high priority queues have been scheduled, but before any low priority queues have been scheduled.

    DISPATCH_QUEUE_PRIORITY_LOW Items dispatched to the queue will run at low priority, i.e. the queue will be scheduled for execution after all default priority and high priority queues have been scheduled.

    DISPATCH_QUEUE_PRIORITY_BACKGROUND Items dispatched to the queue will run at background priority, i.e. the queue will be scheduled for execution after all higher priority queues have been scheduled and the system will run items on this queue on a thread with background status as per setpriority(2) (i.e. disk I/O is throttled and the thread’s scheduling priority is set to lowest value).

    0 讨论(0)
提交回复
热议问题