异步多传

异步多传

920 发布: 2020/1/3 08:10 本文总阅读量

前言

目的:A、B同时执行,拿到A、B的执行结果再实行C

方式一

    NSString *hUrl = @"http://dspceshi.yunbaozb.com/api/public/index.php?service=Video.getVideo&uid=13629&videoid=230&mobileid=123aaa";

    NSLog(@"start");
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_group_async(group, queue, ^{
        sleep(5);
        NSLog(@"同步任务A");
        [YBNetworking postWithUrl:hUrl Dic:nil Suc:^(NSDictionary *data, NSString *code, NSString *msg) {
            dispatch_semaphore_signal(semaphore);
            NSLog(@"net1end===%@",[NSThread currentThread]);
        } Fail:^(id fail) {
//            sleep(5);
           dispatch_semaphore_signal(semaphore);
            NSLog(@"net1fail===%@",[NSThread currentThread]);
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
    });
    
    dispatch_group_async(group, queue, ^{
        sleep(2);
        NSLog(@"同步任务B");
        [YBNetworking postWithUrl:hUrl Dic:nil Suc:^(NSDictionary *data, NSString *code, NSString *msg) {
            dispatch_semaphore_signal(semaphore);
            NSLog(@"net2end===%@",[NSThread currentThread]);
        } Fail:^(id fail) {
//            sleep(10);
           dispatch_semaphore_signal(semaphore);
            NSLog(@"net2fail===%@",[NSThread currentThread]);
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
    });
    
//    dispatch_group_async(group, queue, ^{
//        sleep(10);
//        NSLog(@"同步任务C");
//    });
    
    dispatch_group_notify(group, queue, ^{
        NSLog(@"任务完成执行");
    });

方式二

    NSString *hUrl = @"http://dspceshi.yunbaozb.com/api/public/index.php?service=Video.getVideo&uid=13629&videoid=230&mobileid=123aaa";

    NSLog(@"start");
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        sleep(5);
        NSLog(@"同步任务A");
        [YBNetworking postWithUrl:hUrl Dic:nil Suc:^(NSDictionary *data, NSString *code, NSString *msg) {
            dispatch_group_leave(group);
            NSLog(@"net1end===%@",[NSThread currentThread]);
        } Fail:^(id fail) {
           dispatch_group_leave(group);
            NSLog(@"net1fail===%@",[NSThread currentThread]);
        }];
        
        
    });
    
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        sleep(2);
        NSLog(@"同步任务B");
        [YBNetworking postWithUrl:hUrl Dic:nil Suc:^(NSDictionary *data, NSString *code, NSString *msg) {
            dispatch_group_leave(group);
            NSLog(@"net2end===%@",[NSThread currentThread]);
        } Fail:^(id fail) {
           dispatch_group_leave(group);
            NSLog(@"net2fail===%@",[NSThread currentThread]);
        }];
        
    });
    
//    dispatch_group_async(group, queue, ^{
//        sleep(10);
//        NSLog(@"同步任务C");
//    });
    
    dispatch_group_notify(group, queue, ^{
        NSLog(@"任务完成执行");
    });

总结

方式一、方式二耗时相同