方法一:发射信号量
OC —异步顺序加载
先调用A接口,再调用B接口,再调用C接口
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
网络A请求
中间写block块的网络请求
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
网络B请求
中间写block块的网络请求
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
网络C请求
中间写block块的网络请求
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//刷新页面需要在主线程进行刷新操作
注: 发射信号量,设置无限时去等待,在这里很关键一点,这个等待会阻塞线程,所以切记不要在主线程做这样的操作
方法二:
使用线程队列进行操作
NSOperation
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
NSBlockOperation * o1 = [NSBlockOperation blockOperationWithBlock:^{
block队列1
}];
NSBlockOperation * o2 = [NSBlockOperation blockOperationWithBlock:^{
block队列2
}];
[op1 addDependency:op2];
[queue addOperation:op1];
[queue addOperation:op2];