網(wǎng)絡(luò)塊的學(xué)習(xí)一直是iOS的學(xué)習(xí)開發(fā)難點(diǎn)锭吨,今天就這塊中的難點(diǎn)閑談一下遣疯。洒宝。
廢話不多,先上代碼:
```
//處理多個(gè)事件的并發(fā)行為
- (void)requestForThreeResponse{
? ? __weak typeof(self) weakself = self;
? ? [[UIApplication sharedApplication].keyWindow addSubview:self.hud];
? ? [self.dataArr removeAllObjects];
? ? [self.tipInfoModelsArr removeAllObjects];
? ? //? ? /創(chuàng)建信號(hào)量/
? ? dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
? ? //? ? /創(chuàng)建全局并行/
? ? self.count=3;
? ? 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, ^{
? ? ? ? [weakselfgetAmountMoneySuccess:^(BOOLisSuccess) {
? ? ? ? ? ? if(isSuccess) {
? ? ? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? weakself.count--;
? ? ? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? }
? ? ? ? }];
? ? });
? ? dispatch_group_async(group, queue, ^{
? ? ? ? [weakselfgetFinancialLectureListSuccess:^(BOOLisSuccess) {
? ? ? ? ? ? if(isSuccess) {
? ? ? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? weakself.count--;
? ? ? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? }
? ? ? ? }];
? ? });
? ? dispatch_group_async(group, queue, ^{
? ? ? ? [weakselfrequestForQuestionSuccess:^(BOOLisSuccess) {
? ? ? ? ? ? if(isSuccess) {
? ? ? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? weakself.count--;
? ? ? ? ? ? ? ? dispatch_semaphore_signal(semaphore);
? ? ? ? ? ? }
? ? ? ? }];
? ? });
? ? dispatch_group_notify(group, queue, ^{
? ? ? ? //? ? ? ? /四個(gè)請(qǐng)求對(duì)應(yīng)四次信號(hào)等待/
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? if(weakself.count<=0) {
? ? ? ? ? ? ? ? weakself.isHidden=NO;
? ? ? ? ? ? ? ? _emptyDataHandler.errorDescription = @"網(wǎng)絡(luò)加載出錯(cuò)了讲婚,請(qǐng)查看一下網(wǎng)絡(luò)環(huán)境";
? ? ? ? ? ? ? ? _emptyDataHandler.type = EmptyDataDisplayTypeError;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? _emptyDataHandler.type = EmptyDataDisplayTypeSuccess;
? ? ? ? ? ? ? ? weakself.isHidden=YES;
? ? ? ? ? ? }
? ? ? ? ? ? [weakselfloadNewDataWithHideen:weakself.isHidden];
? ? ? ? ? ? [weakself.scrollView reloadEmptyDataSet];
? ? ? ? });
? ? });
}
```
代碼思路過程:
//? ? /創(chuàng)建信號(hào)量/ ->??/創(chuàng)建全局并行/ ->??/多個(gè)請(qǐng)求對(duì)應(yīng)多次信號(hào)等待/
這樣就可以通過對(duì)網(wǎng)絡(luò)請(qǐng)求封裝的順序及中止控制了