티스토리 뷰

출처

https://developer.apple.com/reference/dispatch/1452933-dispatch_group_notify


dispatch_group_notify

dispatch_group_async 를 통해 이미 group 안에 들어간 job 들이
끝나기를 기다렸다가 모두 완료되면 호출되기를 기대할 때 사용한다.


dispatch_group_notify(<#dispatch_group_t  _Nonnull group#>, <#dispatch_queue_t  _Nonnull queue#>, <#^(void)block#>)




param

1.  job 이 들어가 있는 group 
2. group 안에 job 이 모두 완료되었을 때 완료 block 을 실행시키고 싶은 queue
3. 완료되었을 때 실행시키고 싶은 job block




예제

    dispatch_group_t group = dispatch_group_create();

    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    

    dispatch_group_async(group, globalQueue, ^{

        NSLog(@"job 1 Start!");

        [NSThread sleepForTimeInterval:3];

        NSLog(@"job 1 End!");

    });

    

    dispatch_group_async(group, globalQueue, ^{

        NSLog(@"job 2 Start!");

        [NSThread sleepForTimeInterval:3];

        NSLog(@"job 2 End!");

    });

    

    dispatch_group_async(group, globalQueue, ^{

        NSLog(@"job 3 Start!");

        [NSThread sleepForTimeInterval:3];

        NSLog(@"job 3 End!");

    });

    

    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    dispatch_group_notify(group, mainQueue, ^{

        NSLog(@"Notify Start!");

        

        if ([NSThread isMainThread]) {

            NSLog(@"Here is MainThread");

        } else {

            NSLog(@"Here is Not MainThread");

        }

        

        NSLog(@"Notify End!");

    });

    

    NSLog(@"End!");



결과

TestApplication[57475:1704428] End!
TestApplication[57475:1707148] job 1 Start!
TestApplication[57475:1709626] job 2 Start!
TestApplication[57475:1709627] job 3 Start!
TestApplication[57475:1709626] job 2 End!
TestApplication[57475:1707148] job 1 End!
TestApplication[57475:1709627] job 3 End!
TestApplication[57475:1704428] Notify Start!
TestApplication[57475:1704428] Here is MainThread
TestApplication[57475:1704428] Notify End!








공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함