cool hit counter How to implement this requirement using GCD: three tasks A, B and C are concurrent and execute task D after completion_Intefrankly

How to implement this requirement using GCD: three tasks A, B and C are concurrent and execute task D after completion


The first thing you need to do to solve this is to understand dispatch_group_enter and dispatch_group_leave.

dispatch_group_enter flags a task to be appended to a group and executed once, equal to the number of unexecuted tasks in the group + 1 The dispatch_group_leave flag marks a task as having left the group, executing once, equal to -1 to the number of unexecuted tasks in the group. When the number of unexecuted tasks in the group is 0, only then will dispatch_group_wait unblock, as well as execute the tasks appended to dispatch_group_notify.

    dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    [self requestA:^{
        NSLog(@"--- executeA End of mission---");
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    [self requestB:^{
        NSLog(@"--- executeB End of mission---");
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    [self requestC:^{
        NSLog(@"--- executeC End of mission---");
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, globalQueue, ^{
        [self requestD:^{
            NSLog(@"--- executeD End of mission---");
        }];
    });
- (void)requestA:(void(^)(void))block{
      NSLog(@"--- executeA Start of the mission---");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        block();
    });
}
- (void)requestB:(void(^)(void))block{
      NSLog(@"--- executeB Start of the mission---");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        block();
    });
}
- (void)requestC:(void(^)(void))block{
      NSLog(@"--- executeC Start of the mission---");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        block();
    });
    
}
- (void)requestD:(void(^)(void))block{
    NSLog(@"--- executeD Start of the mission---");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        block();
    });
}

Recommended>>
1、Foxconn is coming to Longhorn A big project
2、A joint counterattack against Apple and Samsung Huaweis most powerful chip puts the world on notice Moving to intelligence across the board
3、The new Touareg L conquering your heart with technology
4、The ninth week of the school calendar we
5、Fomo3D Blockchain System App Development

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号