I want create queue in that first block will run in background, then it finished I want run second block in main thread

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
 // block 1

    dispatch_async(dispatch_get_main_queue(), ^{

  // block 2
      });
    });

How to add queue here?

有帮助吗?

解决方案

What you have, i.e. nested GCD calls, should work fine. It should call the main thread only when the code above the GCD call to the main thread is finished.

其他提示

You can make a queue like this:

dispatch_queue_t queue = dispatch_queue_create("com.company.queue", 0);
dispatch_group_t group = dispatch_group_create();


dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    // block 1

    dispatch_group_async(group, queue, ^{
        printf("first block\n");
    });

    // block 2

    dispatch_group_async(group, queue, ^{
        printf("second block\n");
    });

});

dispatch_group_notify(group, queue, ^{
    printf("all tasks are finished!\n");
});


dispatch_async(dispatch_get_main_queue(), ^{

    // your code on main thread to update UI
    printf("main thread\n");

});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top