Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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");

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top