一个无聊的GCD轮子
面试有回问道,做项目的时候,有没有什么系统的库用着不爽的,然后你自己从新封装的。
<strong><code>GCD</code>有时候就觉得API比较讨厌</strong>
很久之前,撸了一个GCD的便捷操作类。
@interface PTVGCDQueue : NSObject
//< 队列
@property (strong, readonly, nonatomic) dispatch_queue_t dispatchQueue;
//< 主队列
+ (PTVGCDQueue *)mainQueue;
//< 全局队列
+ (PTVGCDQueue *)globalQueue;
+ (PTVGCDQueue *)highPriorityGlobalQueue;
+ (PTVGCDQueue *)lowPriorityGlobalQueue;
+ (PTVGCDQueue *)backgroundPriorityGlobalQueue;
//< 使用才创建的队列
····此处可以添加几个项目中单独使用的线程。
//< 初始化
//< 默认 DISPATCH_QUEUE_SERIAL 队列
- (instancetype)init;
- (instancetype)initSerial;
- (instancetype)initSerialWithLabel:(NSString *)label;
//< 创建DISPATCH_QUEUE_CONCURRENT 队列
- (instancetype)initConcurrent;
- (instancetype)initConcurrentWithLabel:(NSString *)label;
//< 便捷实用
+ (void)executeInMainQueue:(dispatch_block_t)block;
+ (void)executeInGlobalQueue:(dispatch_block_t)block;
+ (void)executeInHighPriorityGlobalQueue:(dispatch_block_t)block;
+ (void)executeInLowPriorityGlobalQueue:(dispatch_block_t)block;
+ (void)executeInBackgroundPriorityGlobalQueue:(dispatch_block_t)block;
+ (void)executeInMainQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
+ (void)executeInGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
+ (void)executeInHighPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
+ (void)executeInLowPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
+ (void)executeInBackgroundPriorityGlobalQueue:(dispatch_block_t)block afterDelaySecs:(NSTimeInterval)sec;
//< 把block加入队列
- (void)execute:(dispatch_block_t)block;
- (void)execute:(dispatch_block_t)block afterDelay:(int64_t)delta;
- (void)execute:(dispatch_block_t)block afterDelaySecs:(float)delta;
- (void)waitExecute:(dispatch_block_t)block;
- (void)barrierExecute:(dispatch_block_t)block;
- (void)waitBarrierExecute:(dispatch_block_t)block;
- (void)suspend;
- (void)resume;
@end
方便集中管理线程,不用记录那么长的API
网友评论