ios的多线程一般有NSOperation和GCD.
NSOperation基本使用:
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
//任务A
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
//任务B
}];
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
//任务C
}];
[operation2 addDependency:operation1];//添加依赖
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
[queue addOperations:@[operation1,operation2,operation3] waitUntilFinished:NO];
GCD基本使用:
dispatch_queue_t queue = dispatch_queue_create("gcd_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
// 任务D
}
});
barrier:
dispatch_barrier_async(queue, ^{
// 任务E
//E会等待D完成后执行,且E完成后才会执行F。其中barrier的queue是create才有这个作用,如果是全局队列作用与dispatch_async一样。
}
});
dispatch_async(queue, ^{
// 任务F
}
});
信号量
//创建信号量并设置计数默认为0
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
//若计数为0则一直等待,当前线程阻塞,信号量减一
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
//与wait配对使用,阻塞的线程会继续执行,信号量加一
dispatch_semaphore_signal(sema);
调度组
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, ^{
//任务1
});
dispatch_group_enter(_group);
dispatch_group_leave(_group);//成对出现添加、移除标记
dispatch_group_notify(group, queue, ^{
//等待任务都执行完后的监听。
});
简单的多线程都很好理解。当实际开发中使用AFN框架时候,因为框架内部网络请求会自动实现多线程,所以在使用时候难免会产生麻烦。有时候会出现一个页面多个网络请求,有依赖关系或无依赖关系。下载功能的app,有可能遇到先并发执行几个任务,再执行一个任务,之后再并发执行几个任务。下面就个人理解写出解决方案,当然不唯一,但可以实现。
一、异步执行A任务,执行后异步执行B任务,然后执行C任务刷新UI。
解决:先利用NSOperation设置依赖关系。保证执行顺序,因为NSOperation默认并发开辟了线程,AFN又开辟线程,所以可以利用信号量阻塞线程达到效果。
__weak typeof (self)weakSelf =self;
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
[weakSelf requestA];
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
[weakSelf requestB];
}];
NSBlockOperation * operation3 = [NSBlockOperation blockOperationWithBlock:^{
[weakSelf requestC];
}];
[operation2 addDependency:operation1];
[operation3 addDependency:operation2];
//保证执行顺序。
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
[queue addOperations:@[operation1,operation2,operation3] waitUntilFinished:NO];
-(void)requestA{
//创建信号量并设置计数默认为0
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[AFNmanager GET:@"" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_semaphore_signal(sema);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//计数+1操作
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
requestB与requestA类似。因为依赖关系保证了执行顺序,AFN开辟新线程后,调用了wait函数,阻塞了requestA的线程,导致B任务无法开启,然而AFN开辟的线程还可以继续执行。请求结束后回调,signal函数使requestA所在线程不再阻塞。转向B任务线程。以此类推,执行到C,C直接主线程刷新UI。
-(void)requestC{
dispatch_async(dispatch_get_main_queue(), ^{
//更新UI操作
});
}
二、异步无序执行A、B任务后执行C任务刷新UI。(两种思路,还有更多不再列举)
1.调度组实现
_group = dispatch_group_create();
[self loadA];
[self loadB];
dispatch_group_notify(_group, dispatch_get_main_queue(), ^{
[self reloadUI];
});
- (void) loadA {//loadB与loadA相似
dispatch_group_enter(_group);
[AFNmanager GET:@"" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_group_leave(_group);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_group_leave(_group);
}];
}
2.调度组异步+信号量实现
- (void) loadData {
// 创建信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
// 创建全局并行
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, ^{
//任务A
[AFNmanager GET:@"" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_semaphore_signal(semaphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_semaphore_signal(semaphore);
}];
});
dispatch_group_async(group, queue, ^{
//任务B
[AFNmanager GET:@"" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_semaphore_signal(semaphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
dispatch_semaphore_signal(semaphore);
}];
});
}
[self loadData];
dispatch_group_notify(group, queue, ^{
// 2个请求对应2次信号等待
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//在这里减2,AB任务完后信号量加了2,才可以继续执行,只要AB中任何一个没有执行完,就会阻塞在wait这。
//在这里 进行请求后的方法,回到主线程
dispatch_async(dispatch_get_main_queue(), ^{
//更新UI操作
});
});
三、多任务并发后执行一个任务后继续多任务并发执行。
dispatch_queue_t queue = dispatch_queue_create("gcd_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{ //任务1 任务2、3等类似于1。
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[AFNmanager GET:@"" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
dispatch_semaphore_signal(semaphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//计数+1操作
dispatch_semaphore_signal(semaphore);
}];
//阻塞任务1所在线程,等待执行结束后不再阻塞
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
});
dispatch_barrier_async(queue, ^{
//前面的并发任务执行完后执行 内部类似于任务1.
}
});
dispatch_async(queue, ^{
// 后面的并发任务加不加信号量控制都可以。
}
});