最近做了一个需求,针对一批数据,进行网络请求,请求结束后,根据返回结果,更新数据库.
这种情况肯定是利用事务,批量更新比较合理。
同时网络请求封装是异步执行,结果在回调里处理。这个时候GCD就派上用场了。
Dispatch Group机制这个使用法参照 《编写高质量iOS与OS X代码的52个有效方法》--第六章 第44条
44.通过Dispatch Group机制,根据系统资源状况来执行任务
dispatch group
NSMutableArray * realnameOrderInfoModelArray = [[NSMutableArray alloc]init];
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
[self.successPrintInfoArray enumerateObjectsUsingBlock:^(YDOrderInfoModel * orderModel, NSUInteger idx, BOOL * _Nonnull stop) {
dispatch_group_enter(group);
dispatch_group_async(group, queue, ^{
YDRealnameOrderInfoModel * realnameOrderInfoModel = [[YDRealnameOrderInfoModel alloc] initWithOrderInfoModel:orderModel];
[YDUploadRealNameDataService uploadNewRealNameInfoWithDataArr:@[realnameOrderInfoModel] Success:^(BOOL result) {
if(result)
{
realnameOrderInfoModel.isUpload = @"1";
}
else
{
realnameOrderInfoModel.isUpload = @"-1";
}
[realnameOrderInfoModelArray addObject:realnameOrderInfoModel];
dispatch_group_leave(group);
} failure:^(NSString *error) {
[realnameOrderInfoModelArray addObject:realnameOrderInfoModel];
dispatch_group_leave(group);
}];
});
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^(){
//等数据处理完了再进行入库.
[[YDOrderTool sharedOrderTool] addNewRealNameOrderDataWithArray:realnameOrderInfoModelArray];
});
NSOperationQueue
// 创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 任务1
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
UIImage * image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]]];
NSLog(@"任务1 完成,线程:%@", [NSThread currentThread]);
}];
// 任务2
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
UIImage * image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.url1]]];
NSLog(@"任务2 完成,线程:%@", [NSThread currentThread]);
}];
// 任务3
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
UIImage * image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.url1]]];
NSLog(@"任务3 完成,线程:%@", [NSThread currentThread]);
}];
// 添加操作依赖,注意不能循环依赖
[op1 addDependency:op2];
[op3 addDependency:op1];
op3.completionBlock = ^{
NSLog(@"全部完成,线程:%@", [NSThread currentThread]);
};
// 添加操作到队列
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
其实操作队列只是提供了一套高层的OC API。这里需要需要注意
The receiver is not considered ready to execute until all of its dependent operations have finished executing. If the receiver is already executing its task, adding dependencies has no practical effect. This method may change the isReady and dependencies properties of the receiver. It is a programmer error to create any circular dependencies among a set of operations. Doing so can cause a deadlock among the operations and may freeze your program.
1.循环依赖项会导致操作之间的死锁。
2.直到接收方的所有相关操作都完成后,才认为接收方已经准备好执行。如果接收方已经在执行其任务,那么添加依赖项没有实际效果
NSOperation和NSOperationQueue相关
dispatch_semaphore
思路有点类似dispatch_group_enter来添加计数
{
dispatch_group_t group = dispatch_group_create();
for(int i = 0 ; i< 10; i ++)
{
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:self.url1] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
});
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"全部完成,线程:%@", [NSThread currentThread]);
}
其实用dispatch_group_wait 与 dispatch_group_notify来执行后面的操作都可以,但是dispatch_group_notify可以直接将要执行的动作传入块里。等dispatch_group执行完后 会在执行线程执行。 加入当前线程不想堵塞,而开发者又想在任务结束之后得到通知,dispatch_group_notify就更方便了。
iOS GCD之dispatch_semaphore学习