iOS 多個非同步網路請求全部返回後再執行具體邏輯的方法

來源:互聯網
上載者:User

標籤:

先看一段錯誤的方法:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSURLSession *session = [NSURLSession sharedSession];        dispatch_queue_t dispatchQueue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);    dispatch_group_t dispatchGroup = dispatch_group_create();    dispatch_group_async(dispatchGroup, dispatchQueue, ^(){                NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {            NSLog(@"got data from internet1");        }];        [task resume];                            });    dispatch_group_async(dispatchGroup, dispatchQueue, ^(){        NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {            NSLog(@"got data from internet2");        }];        [task resume];    });        dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){        NSLog(@"end");    });}

看看log的輸出

2016-07-13 17:42:55.170 aaaa[4797:295528] end2016-07-13 17:42:55.322 aaaa[4797:295574] got data from internet22016-07-13 17:42:55.375 aaaa[4797:295574] got data from internet1

完全沒有達到效果。這是因為這裡的網路請求是個非同步方法,沒有等待具體的資料返回,放入的dispatch queue的 block就執行完畢了。所以沒收到2個網路資料,就提前調用了dispatch_group_notify指定的結束方法。

再看正確的方法:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        NSURLSession *session = [NSURLSession sharedSession];        dispatch_queue_t dispatchQueue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);    dispatch_group_t dispatchGroup = dispatch_group_create();    // dispatch_group_async(dispatchGroup, dispatchQueue, ^(){        dispatch_group_enter(dispatchGroup);        NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        NSLog(@"got data from internet1");                dispatch_group_leave(dispatchGroup);    }];    [task resume];        //  });    //  dispatch_group_async(dispatchGroup, dispatchQueue, ^(){    dispatch_group_enter(dispatchGroup);        NSURLSessionDataTask *task2 = [session dataTaskWithURL:[NSURL URLWithString:@"https://www.baidu.com"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        NSLog(@"got data from internet2");                dispatch_group_leave(dispatchGroup);    }];    [task2 resume];    // });        dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){        NSLog(@"end");    });}

看正確是輸出結果:

2016-07-13 17:46:10.282 aaaa[4847:300370] got data from internet12016-07-13 17:46:10.501 aaaa[4847:300370] got data from internet22016-07-13 17:46:10.502 aaaa[4847:300341] end

 

我們看看關於dispatch_group_enter 的具體說明

Calling this function increments the current count of outstanding tasks in the group. Using this function (with dispatch_group_leave) allows your application to properly manage the task reference count if it explicitly adds and removes tasks from the group by a means other than using the dispatch_group_async function. A call to this function must be balanced with a call to dispatch_group_leave. You can use this function to associate a block with more than one group at the same time.

簡單的說,就是dispatch_group_enter會對group的內部計數加一,dispatch_group_leave會對group的內部計數減一,就類似以前的retain和release方法。說白了也是維護了一個計數器,只不過系統將這個計數器封裝了起來,不用我們自己實現了,代碼看起來更簡潔,高大上了。

 

iOS 多個非同步網路請求全部返回後再執行具體邏輯的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.