GCD網絡請求:無序(異步+并發)有序(異步+串行)

GCD:異步+并發(信號量)

requestNetworking要保證請求完成才可以再次請求加個判斷:isFinishLoading

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self requestNetworking];
}

-(void)requestNetworking{
    dispatch_group_t dispatchGroup = dispatch_group_create();
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //第一個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group1--------%@",[NSThread currentThread]);
        [self test1];
    });
    
    //第二個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group2--------%@",[NSThread currentThread]);
        [self test2];
    });
    
    //第三個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group3--------%@",[NSThread currentThread]);
        [self test3];
    });
    
    //第四個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group4--------%@",[NSThread currentThread]);
        [self test4];
    });
    
    //第五個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group5--------%@",[NSThread currentThread]);
        [self test5];
    });
    
    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
        NSLog(@"--------updateUi--------%@",[NSThread currentThread]);
    });
}

-(void)test1{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第一步網絡請求完成");
        dispatch_semaphore_signal(semaphore);
    }];
    [task resume];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

-(void)test2{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.jisilu.cn"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第二步網絡請求完成");
        dispatch_semaphore_signal(semaphore);
    }];
    [task resume];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

-(void)test3{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第三步網絡請求完成");
        dispatch_semaphore_signal(semaphore);
    }];
    [task resume];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

-(void)test4{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://developers.adnet.qq.com/doc/ios/guide"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第四步網絡請求完成");
        dispatch_semaphore_signal(semaphore);
    }];
    [task resume];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

-(void)test5{
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://bugly.qq.com/v2"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第五步網絡請求完成");
        dispatch_semaphore_signal(semaphore);
    }];
    [task resume];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
2023-04-28 11:30:52.339737+0800 text_04[3758:109469] --------group1--------<NSThread: 0x60000024c740>{number = 13, name = (null)}
2023-04-28 11:30:52.339833+0800 text_04[3758:109451] --------group2--------<NSThread: 0x600000247500>{number = 9, name = (null)}
2023-04-28 11:30:52.339854+0800 text_04[3758:109449] --------group3--------<NSThread: 0x60000021c6c0>{number = 7, name = (null)}
2023-04-28 11:30:52.339864+0800 text_04[3758:109470] --------group4--------<NSThread: 0x600000250540>{number = 15, name = (null)}
2023-04-28 11:30:52.339890+0800 text_04[3758:109453] --------group5--------<NSThread: 0x60000025df80>{number = 3, name = (null)}
2023-04-28 11:30:52.364842+0800 text_04[3758:109454] 第一步網絡請求完成
2023-04-28 11:30:52.399705+0800 text_04[3758:109456] 第五步網絡請求完成
2023-04-28 11:30:52.428851+0800 text_04[3758:109456] 第四步網絡請求完成
2023-04-28 11:30:52.430836+0800 text_04[3758:109470] 第二步網絡請求完成
2023-04-28 11:30:53.123856+0800 text_04[3758:109451] 第三步網絡請求完成
2023-04-28 11:30:53.124068+0800 text_04[3758:109415] --------updateUi--------<_NSMainThread: 0x600000218440>{number = 1, name = main}

在這個示例中,我們使用dispatch_group_async()函數將每個請求添加到dispatch_group_t中。對于每個請求,我們在一個dispatch_queue_t中執行它,并使用dispatch_semaphore_t在請求完成時釋放信號量。我們在每個請求的末尾使用dispatch_semaphore_wait()函數等待信號量,以確保在處理完每個請求之前不會離開dispatch_group_t。最后,我們調用dispatch_group_notify()函數來指定所有請求完成后要執行的代碼塊。

在這個示例中,dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER):放在子線程,不會阻塞主線程,可以執行其他操作。


GCD:異步+并發(dispatch_group_enter)推薦用這個目前項目在使用

requestNetworking要保證請求完成才可以再次請求加個判斷:isFinishLoading

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self requestNetworking];
}

-(void)requestNetworking{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.group = dispatch_group_create();

    //第一個網絡請求
    dispatch_group_enter(self.group);
    dispatch_group_async(self.group, queue, ^{
        NSLog(@"--------group1--------%@",[NSThread currentThread]);
        [self test1];
    });

    //第二個網絡請求
    dispatch_group_enter(self.group);
    dispatch_group_async(self.group, queue, ^{
        NSLog(@"--------group2--------%@",[NSThread currentThread]);
        [self test2];
    });

    //第三個網絡請求
    dispatch_group_enter(self.group);
    dispatch_group_async(self.group, queue, ^{
        NSLog(@"--------group3--------%@",[NSThread currentThread]);
        [self test3];
    });

    //第四個網絡請求
    dispatch_group_enter(self.group);
    dispatch_group_async(self.group, queue, ^{
        NSLog(@"--------group4--------%@",[NSThread currentThread]);
        [self test4];
    });

    //第五個網絡請求
    dispatch_group_enter(self.group);
    dispatch_group_async(self.group, queue, ^{
        NSLog(@"--------group5--------%@",[NSThread currentThread]);
        [self test5];
    });

    dispatch_group_notify(self.group, dispatch_get_main_queue(), ^{
        NSLog(@"--------updateUi--------%@",[NSThread currentThread]);
    });
}

-(void)test1{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第一步網絡請求完成");
        dispatch_group_leave(self.group);
    }];
    [task resume];
}

-(void)test2{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.jisilu.cn"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第二步網絡請求完成");
        dispatch_group_leave(self.group);
    }];
    [task resume];
}

-(void)test3{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第三步網絡請求完成");
        dispatch_group_leave(self.group);
    }];
    [task resume];
}

-(void)test4{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://developers.adnet.qq.com/doc/ios/guide"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第四步網絡請求完成");
        dispatch_group_leave(self.group);
    }];
    [task resume];
}

-(void)test5{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://bugly.qq.com/v2"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第五步網絡請求完成");
        dispatch_group_leave(self.group);
    }];
    [task resume];
}
2023-04-28 11:28:50.619371+0800 text_04[3663:105006] --------updateUi--------<_NSMainThread: 0x60000227c440>{number = 1, name = main}
2023-04-28 11:28:50.619658+0800 text_04[3663:105186] --------group1--------<NSThread: 0x600002271e00>{number = 7, name = (null)}
2023-04-28 11:28:50.619718+0800 text_04[3663:105187] --------group2--------<NSThread: 0x600002270f40>{number = 4, name = (null)}
2023-04-28 11:28:50.619749+0800 text_04[3663:105191] --------group3--------<NSThread: 0x600002220300>{number = 5, name = (null)}
2023-04-28 11:28:50.619763+0800 text_04[3663:105188] --------group4--------<NSThread: 0x60000221ea00>{number = 9, name = (null)}
2023-04-28 11:28:50.619794+0800 text_04[3663:105190] --------group5--------<NSThread: 0x600002230c80>{number = 3, name = (null)}
2023-04-28 11:28:50.652971+0800 text_04[3663:105187] 第一步網絡請求完成
2023-04-28 11:28:50.704591+0800 text_04[3663:105190] 第四步網絡請求完成
2023-04-28 11:28:50.709022+0800 text_04[3663:105190] 第二步網絡請求完成
2023-04-28 11:28:50.752817+0800 text_04[3663:105186] 第五步網絡請求完成
2023-04-28 11:28:51.462473+0800 text_04[3663:105187] 第三步網絡請求完成
2023-04-28 11:28:51.462692+0800 text_04[3663:105006] --------updateUi--------<_NSMainThread: 0x60000227c440>{number = 1, name = main}

在上面的代碼中,我們首先創建了一個dispatch_group_t類型的組,并將它添加到全局隊列中。然后,我們使用dispatch_group_enter函數進入組,并分別發起了三個異步網絡請求,并在每個請求完成后打印出完成信息,并調用dispatch_group_leave函數以便離開組。最后,我們使用dispatch_group_notify函數在所有請求完成后打印出“所有異步網絡請求已完成”的信息。


GCD:異步+串行(信號量,開子線程按順序執行)

requestNetworking要保證請求完成才可以再次請求加個判斷:isFinishLoading

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self requestNetworking];
}

-(void)requestNetworking{
    dispatch_group_t dispatchGroup = dispatch_group_create();
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.semaphore = dispatch_semaphore_create(0);

    //第一個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group1--------%@",[NSThread currentThread]);
        [self test1];
    });
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);

    //第二個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group2--------%@",[NSThread currentThread]);
        [self test2];
    });
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);

    //第三個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group3--------%@",[NSThread currentThread]);
        [self test3];
    });
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);

    //第四個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group4--------%@",[NSThread currentThread]);
        [self test4];
    });
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);

    //第五個網絡請求
    dispatch_group_async(dispatchGroup, dispatchQueue, ^{
        NSLog(@"--------group5--------%@",[NSThread currentThread]);
        [self test5];
    });
    dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);

    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
        NSLog(@"--------updateUi--------%@",[NSThread currentThread]);
    });
}

-(void)test1{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第一步網絡請求完成");
        dispatch_semaphore_signal(self.semaphore);
    }];
    [task resume];
}

-(void)test2{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.jisilu.cn"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第二步網絡請求完成");
        dispatch_semaphore_signal(self.semaphore);
    }];
    [task resume];
}

-(void)test3{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第三步網絡請求完成");
        dispatch_semaphore_signal(self.semaphore);
    }];
    [task resume];
}

-(void)test4{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://developers.adnet.qq.com/doc/ios/guide"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第四步網絡請求完成");
        dispatch_semaphore_signal(self.semaphore);
    }];
    [task resume];
}

-(void)test5{
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://bugly.qq.com/v2"]];
    NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"第五步網絡請求完成");
        dispatch_semaphore_signal(self.semaphore);
    }];
    [task resume];
}

2023-04-28 15:20:02.338944+0800 text_04[2325:54425] --------group1--------<NSThread: 0x600000dc4280>{number = 7, name = (null)}
2023-04-28 15:20:02.356631+0800 text_04[2325:54425] 第一步網絡請求完成
2023-04-28 15:20:02.356897+0800 text_04[2325:54422] --------group2--------<NSThread: 0x600000dc9140>{number = 9, name = (null)}
2023-04-28 15:20:02.439213+0800 text_04[2325:54425] 第二步網絡請求完成
2023-04-28 15:20:02.439444+0800 text_04[2325:54421] --------group3--------<NSThread: 0x600000dd5d00>{number = 3, name = (null)}
2023-04-28 15:20:03.513870+0800 text_04[2325:54421] 第三步網絡請求完成
2023-04-28 15:20:03.514117+0800 text_04[2325:54421] --------group4--------<NSThread: 0x600000dd5d00>{number = 3, name = (null)}
2023-04-28 15:20:03.552748+0800 text_04[2325:54421] 第四步網絡請求完成
2023-04-28 15:20:03.552924+0800 text_04[2325:54425] --------group5--------<NSThread: 0x600000dc4280>{number = 7, name = (null)}
2023-04-28 15:20:03.685218+0800 text_04[2325:54421] 第五步網絡請求完成
2023-04-28 15:20:03.685468+0800 text_04[2325:54236] --------updateUi--------<_NSMainThread: 0x600000d94440>{number = 1, name = main}

在上面的示例中,每個網絡請求都是異步執行的,并按順序執行。在每個網絡請求執行完成后,都通過dispatch_semaphore_signal方法使dispatch_semaphore_t對象的計數器加1。在每個網絡請求執行之前,都需要通過dispatch_semaphore_wait方法等待dispatch_semaphore_t對象的計數器達到相應的值。一旦所有請求完成,將會處理所有請求的結果。

在上面的示例中,dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER):放在主線程,這個會阻塞主線程,其他操作執行不了。只有當執行完網絡請求才能做其他事情


信號量

dispatch_semaphore_signal是GCD信號量函數之一,它的作用是增加信號量的計數器,從而解除由dispatch_semaphore_wait函數所阻塞的線程。

dispatch_semaphore_signal的參數是一個dispatch_semaphore_t對象,它是由dispatch_semaphore_create函數創建的。每次調用dispatch_semaphore_signal函數,都會將信號量計數器加1。如果有線程在等待信號量,那么其中的一個線程會被解除阻塞,繼續執行后續的代碼

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // 需要等待信號量
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    NSLog(@"Task completed.");
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // 執行任務
    NSLog(@"Task executing...");
    // 增加信號量計數器的值,解除阻塞
    dispatch_semaphore_signal(semaphore);
});

在上面的代碼中,第一個線程會等待信號量,直到第二個線程調用dispatch_semaphore_signal函數增加信號量計數器的值。這時,第一個線程就可以繼續執行后續的代碼,輸出Task completed.的日志信息。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容