for 异步任务需要将当前线程阻挡住,
这个时候考虑到信号量:dispatch_semaphore_t。
但另一个思路:lock行不行呢?如果用nslock呢。在for 外部定于一个lock。循环内开始的是 lock on,异步任务结束后 Lock off。这个思路还没验证。
今天细想,lock 可能无效。
必须加入阻塞才能隔断循环。
而do-while 是一个很好的方案。
nslock *lock = [lock init];
for(:in){
[lock on ];
doyourthinghere^{//这是循环内部的异步任务
after for a while ;
your thing done;
[lock off];
};//
do{
do nothing or anything if your want do print
}while(lock is on)
}
{
__block NSLock *lock ;
__block BOOL isLock;
}
-(void)doatest{
lock= [[NSLock alloc]init];
isLock= NO;
dispatch_queue_t queue = dispatch_queue_create("labelqueue", DISPATCH_QUEUE_SERIAL);//dispatch_get_main_queue();//
for (int i = 0; i<10; i++) {
[lock lock];
isLock = YES;
dispatch_async(queue, ^{
NSRunLoop *rllop = [NSRunLoop currentRunLoop];
NSLog(@"currentRunLoop :%@",rllop);
NSTimer *tierm = [NSTimer scheduledTimerWithTimeInterval:3 repeats:NO block:^(NSTimer * _Nonnull timer) {
NSLog(@"time??");
// [lock unlock];
// isLock = NO;
}];
[tierm fire];
[rllop addTimer:tierm forMode:NSRunLoopCommonModes];
NSLog(@"targe start:%d",i);
[self performSelector:@selector(donting) withObject:nil afterDelay:2];
[rllop run];
});
do {
// NSLog(@"dont dont anythign herer ");
} while (isLock);
}
}
下面这个也行。
{
__block NSLock *lock ;
__block BOOL isLock;
}
-(void)doatest2{
lock= [[NSLock alloc]init];
isLock= NO;
dispatch_queue_t serialQueue = dispatch_queue_create("labelqueue", DISPATCH_QUEUE_SERIAL);//dispatch_get_main_queue();//
dispatch_async(serialQueue, ^{
for (int i = 0; i<10; i++) {
[lock lock];
isLock = YES;
NSLog(@"targe start:%d",i);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@" end this i :%d",i);
// [lock unlock];
[self donting];
});
do {
// NSLog(@"dont dont anythign herer ");
} while (isLock);
}
});
}
-(void)donting{
NSLog(@"TTTTTLIW");
[lock unlock];
isLock = NO;
}
上面简化为:(不用外部变量)
-(void)doatest{
NSLock *lock = [[NSLock alloc]init];
__block BOOL isLock = NO;
dispatch_queue_t queue = dispatch_queue_create("labelqueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
for (int i = 0; i<10; i++) {
[lock lock];
isLock = YES;
NSLog(@"targe start:%d",i);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@" end this i :%d",i);
[lock unlock];
isLock = NO;
});
do {
NSLog(@"dont dont anythign herer ");
} while (isLock);
}
});
}
最后看看信号量的处理方案:
-(void)dota{
NSLog(@"处理前创建信号量,开始循环异步请求操作\n");
// 1.创建一个串行队列,保证for循环依次执行
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
// 2.异步执行任务
dispatch_async(serialQueue, ^{
// 3.创建一个数目为1的信号量,用于“卡”for循环,等上次循环结束在执行下一次的for循环
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
for (int i = 0; i<5; i++) {
// 开始执行for循环,让信号量-1,这样下次操作须等信号量>=0才会继续,否则下次操作将永久停止
NSLog(@"信号量等待中\n");
// 模拟一个异步任务
// NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://github.com"]];
// NSURLSession *session = [NSURLSession sharedSession];
// NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// // 本次for循环的异步任务执行完毕,这时候要发一个信号,若不发,下次操作将永远不会触发
// [tampArray addObject:@(i)];
// NSLog(@"本次耗时操作完成,信号量+1 %@\n",[NSThread currentThread]);
// dispatch_semaphore_signal(sema);
//
// }];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"本次耗时操作完成,信号量+1 %@\n",[NSThread currentThread]);
dispatch_semaphore_signal(sema);
});
// [dataTask resume];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
NSLog(@"其他操作");
// for (NSNumber *num in tampArray) {
// NSLog(@"所有操作完成后的操作---> %@\n",num);
// }
});
}
网友评论