문제

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