TokenOperationQueue
TokenOperationQueue提供了一种易于使用带优先级的线程并解决线程过多的解决方案。
使用CocoaPods安装
CocoaPods是一个Objective-C的依赖管理器,它自动并简化了在项目中使用第三方库(如TokenOperationQueue)的过程。您可以使用以下命令安装它
$ gem install cocoapods
Podfile
要使用CocoaPods将TokenOperationQueue集成到您的Xcode项目中,在Podfile中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
target 'TargetName' do
pod 'TokenOperationQueue'
end
然后,运行以下命令
$ pod install
架构
- TokenOperationGroup
-
- TokenOperationGroup+Chain
- TokenOperationQueue
-
- TokenOperationQueue+Chain
- TokenOperationTool
- TokenRenderQueue
- TokenSemaphore
- TokenSerialQueue
- TokenSerialQueue+Chain
TokenOperationGroup
基于NSOperationQueue的组任务解决方案
TokenOperationQueue
一个全局并发队列(GCD包装函数),允许您设置最大并发数(最大数量必须 >= 2)
TokenOperationQueue可以通过queue.chain_waitUntilFinished()方法阻塞当前线程,直到操作对象完成任务
TokenOperationQueue可以通过queue.chain_cancelAllOperations取消所有非运行中的操作
非常容易使用!
TokenOperationTool
一些有用的工具。
TokenSemaphore
dispatch_semaphore_t包装器,允许您优雅地使用信号量。
TokenSerialQueue
一个DISPATCH_QUEUE_SERIAL的包装器,允许您阻塞当前线程执行,直到操作对象完成任务或停止执行非运行中的操作。
用法
队列
并行运行操作
代码
TokenOperationQueue
.sharedQueue
.chain_runOperation(^{
NSLog(@"1s");
sleep(1);
NSLog(@"1e");
})
.chain_runOperation(^{
NSLog(@"2s");
sleep(2);
NSLog(@"2e");
})
.chain_runOperation(^{
NSLog(@"3s");
sleep(3);
NSLog(@"3e");
})
.chain_runOperation(^{
NSLog(@"4s");
sleep(3);
NSLog(@"4e");
})
.chain_runOperation(^{
NSLog(@"5s");
sleep(2);
NSLog(@"5e");
})
.chain_runOperation(^{
NSLog(@"6s");
sleep(1);
NSLog(@"6e");
});
输出
1s
3s
2s
4s
5s
6s
1e
6e
5e
2e
4e
3e
带优先级的并行运行操作
代码
TokenOperationQueue
.sharedQueue
.chain_runOperationWithPriority(TokenQueuePriorityBackground, ^{
NSLog(@"1s");
sleep(3);
NSLog(@"1e");
})
.chain_runOperationWithPriority(TokenQueuePriorityBackground, ^{
NSLog(@"1s");
sleep(3);
NSLog(@"1e");
})
.chain_runOperationWithPriority(TokenQueuePriorityBackground, ^{
NSLog(@"1s");
sleep(3);
NSLog(@"1e");
})
.chain_runOperationWithPriority(TokenQueuePriorityLow, ^{
NSLog(@"2s");
sleep(5);
NSLog(@"2e");
})
.chain_runOperationWithPriority(TokenQueuePriorityLow, ^{
NSLog(@"2s");
sleep(5);
NSLog(@"2e");
})
.chain_runOperationWithPriority(TokenQueuePriorityLow, ^{
NSLog(@"2s");
sleep(5);
NSLog(@"2e");
})
.chain_runOperationWithPriority(TokenQueuePriorityDefault, ^{
NSLog(@"3s");
sleep(1);
NSLog(@"3e");
})
.chain_runOperationWithPriority(TokenQueuePriorityDefault, ^{
NSLog(@"3s");
sleep(1);
NSLog(@"3e");
})
.chain_runOperationWithPriority(TokenQueuePriorityDefault, ^{
NSLog(@"3s");
sleep(1);
NSLog(@"3e");
})
.chain_runOperationWithPriority(TokenQueuePriorityHigh, ^{
NSLog(@"4s");
sleep(1);
NSLog(@"4e");
})
.chain_runOperationWithPriority(TokenQueuePriorityHigh, ^{
NSLog(@"4s");
sleep(1);
NSLog(@"4e");
})
.chain_runOperationWithPriority(TokenQueuePriorityHigh, ^{
NSLog(@"4s");
sleep(1);
NSLog(@"4e");
});
输出
4s
4s
4s
4e
4e
4e
3s
3s
3s
3e
3e
3e
2s
2s
2s
2e
2e
2e
1s
1s
1s
1e
1e
1e
等待直到完成
代码
TokenOperationQueue
.sharedQueue
.chain_runOperation(^{
sleep(6);
NSLog(@"1");
})
.chain_runOperation(^{
sleep(2);
NSLog(@"2");
})
.chain_runOperation(^{
sleep(1);
NSLog(@"3");
})
.chain_runOperation(^{
sleep(1);
NSLog(@"4");
})
.chain_runOperation(^{
sleep(1);
NSLog(@"5");
})
.chain_waitUntilFinished();
NSLog(@"done");
输出
4
5
3
2
1
done
取消所有操作
代码
TokenOperationQueue *queue = TokenOperationQueue
.sharedQueue
.chain_setMaxConcurrent(2)
.chain_runOperationWithPriority(TokenQueuePriorityBackground, ^{
NSLog(@"1s");
sleep(3);
NSLog(@"1e");
})
.chain_runOperation(^{
NSLog(@"1s");
sleep(2);
NSLog(@"1e");
})
.chain_runOperation(^{
NSLog(@"2s");
sleep(2);
NSLog(@"2e");
})
.chain_runOperation(^{
NSLog(@"3s");
sleep(2);
NSLog(@"3e");
})
.chain_runOperation(^{
NSLog(@"4s");
sleep(2);
NSLog(@"4e");
})
.chain_runOperation(^{
NSLog(@"5s");
sleep(2);
NSLog(@"5e");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"ready to cancel");
queue.chain_cancelAllOperations();
NSLog(@"canceled");
});
输出
1s
2s
ready to cancel
canceled
1e
2e
分组
使用分组
代码
TokenOperationGroup
.group
.chain_setMaxConcurrent(2)
.chain_addOperation(^{
NSLog(@"1s");
sleep(1);
NSLog(@"1e");
})
.chain_addOperation(^{
NSLog(@"2");
})
.chain_addOperation(^{
NSLog(@"3");
})
.chain_addOperation(^{
NSLog(@"4");
})
.chain_setCompletion(^{
NSLog(@"done");
})
.chain_run();
输出
1s
2
3
4
1e
done
带优先级的分组
代码
TokenOperationGroup
.group
.chain_setMaxConcurrent(2)
.chain_addOperationWithPriority(NSOperationQueuePriorityVeryLow, ^{
NSLog(@"1");
})
.chain_addOperationWithPriority(NSOperationQueuePriorityLow, ^{
NSLog(@"2");
})
.chain_addOperationWithPriority(NSOperationQueuePriorityNormal, ^{
NSLog(@"3");
})
.chain_addOperationWithPriority(NSOperationQueuePriorityHigh, ^{
NSLog(@"4");
})
.chain_addOperationWithPriority(NSOperationQueuePriorityVeryHigh, ^{
NSLog(@"5");
})
.chain_setCompletion(^{
NSLog(@"done");
})
.chain_run();
输出
4
5
2
3
1
done
分组取消
代码
TokenOperationGroup *group = TokenOperationGroup
.group
.chain_setMaxConcurrent(1)
.chain_addOperation(^{
NSLog(@"1s");
sleep(1);
NSLog(@"1e");
})
.chain_addOperation(^{
NSLog(@"2s");
sleep(1);
NSLog(@"2e");
})
.chain_addOperation(^{
NSLog(@"3s");
sleep(1);
NSLog(@"3e");
})
.chain_addOperation(^{
NSLog(@"4s");
sleep(1);
NSLog(@"4e");
})
.chain_addOperation(^{
NSLog(@"5s");
sleep(1);
NSLog(@"5e");
})
.chain_setCompletion(^{
NSLog(@"done");
})
.chain_run();
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"ready to cancel");
group.chain_cancel();
NSLog(@"canceled");
});
输出
1s
1e
2s
2e
3s
ready to cancel
canceled
3e