iOS 判断多个子线程都执行完成

判断A、B、C、D四个子线程都执行完成。

- (void)requestDataList
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  
    dispatch_group_t group = dispatch_group_create();

    dispatch_group_async(group, queue, ^{
        [self request1];
    });
    
    dispatch_group_async(group, queue, ^{
        [self request2];
    });
    
    dispatch_group_async(group, queue, ^{
        [self request3];
    });
    
    dispatch_group_async(group, queue, ^{
        [self request4];
    });
    dispatch_notify(group, queue, ^{
       NSLog(@"全部的线程都执行完了");
        
    });
}
#pragma mark - 接口

- (void)request1
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"type"] = @"1";
    params[@"id"] = @"0";

    dispatch_semaphore_t  sema = dispatch_semaphore_create(0);

    [[RequestClient sharedInstance] requestWithMethod:GET
                                         relativePath:@"banner/findByTypeAndGuideId"
                                           parameters:params
                                              success:^(NSDictionary *responseObject) {
                                                  NSLog(@"---1");
                                                  dispatch_semaphore_signal(sema);
                                              }
                                              failure:^(NSError *error) {
                                              }];
    
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

- (void)request2
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"payStatus"] = @"1";
    params[@"page"] = @"1";
    params[@"limit"] = @"10";
    
    dispatch_semaphore_t  sema = dispatch_semaphore_create(0);

    [[RequestClient sharedInstance] requestWithMethod:GET
                                         relativePath:@"order/carouselOrder"
                                           parameters:params
                                              success:^(NSDictionary *responseObject) {
                                                  NSLog(@"---2");
                                                  dispatch_semaphore_signal(sema);
                                              }
                                              failure:^(NSError *error) {
                                              }];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}

- (void)request3
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"location"] = [UserModel sharedInstance].city;
    params[@"type"] = STR_NUM(1);
    params[@"startTime"] = [UserModel sharedInstance].homeDate[@"beginDate"];
    params[@"page"] = STR_NUM(1);
    params[@"limit"] = STR_NUM(5);
    
    dispatch_semaphore_t  sema = dispatch_semaphore_create(0);
    
    [[RequestClient sharedInstance] requestWithMethod:GET
                                         relativePath:@"guide/sortTop10ByLocation"
                                           parameters:params
                                              success:^(NSDictionary *responseObject) {
                                                  NSLog(@"---3");
                                                  dispatch_semaphore_signal(sema);
                                              }
                                              failure:^(NSError *error) {
                                              }];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}

- (void)request4
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"type"] = @"0";
    params[@"serveType"] = @"0";
    params[@"address"] = [UserModel sharedInstance].city;
    params[@"mustPlay"] = STR_NUM(1);
    params[@"guideId"] = @"0";
    params[@"guideServeId"] =  @"0";
    params[@"page"] = STR_NUM(self.page);
    params[@"limit"] = STR_NUM(10);
    
    dispatch_semaphore_t  sema = dispatch_semaphore_create(0);

    [[RequestClient sharedInstance] requestWithMethod:GET
                                         relativePath:@"serve/guideServeFilterList"
                                           parameters:params
                                              success:^(NSDictionary *responseObject) {
                                                  NSLog(@"---4");
                                                  dispatch_semaphore_signal(sema);
                                              }
                                              failure:^(NSError *error) {
                                              }];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}
//输出结果

2019-04-28 21:34:15.224569+0800 NaJiuZou[43491:1251622] ---1
2019-04-28 21:34:15.372758+0800 NaJiuZou[43491:1251622] ---2
2019-04-28 21:34:15.462564+0800 NaJiuZou[43491:1251622] ---3
2019-04-28 21:34:16.816414+0800 NaJiuZou[43491:1251622] ---4
2019-04-28 21:34:16.816636+0800 NaJiuZou[43491:1251682] 全部的线程都执行完了

猜你喜欢

转载自blog.csdn.net/u010545480/article/details/89646377