Quickies

[categories] [index] [all (527)] [latest]

Cocoa Foundation
  1. dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"-> 1");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"<- 1");
        dispatch_group_leave(group);
    });
    
    dispatch_group_enter(group);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"-> 2");
        [NSThread sleepForTimeInterval:4];
        NSLog(@"<- 2");
        dispatch_group_leave(group);
    });
    
    dispatch_group_enter(group);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"-> 3");
        [NSThread sleepForTimeInterval:3];
        NSLog(@"<- 3");
        dispatch_group_leave(group);
    });
    
    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSLog(@"finished");
    });
    
    [[NSRunLoop currentRunLoop] run];
    

    Logs:

    -> 1
    -> 2
    -> 3
    <- 1
    <- 3
    <- 2
    finished