NSOperationQueue

NSOperationQueue

/// queue上的任务的执行进度.(执行完了才能算)
@property (readonly, strong) NSProgress *progress API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));

使用用例:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.progress.totalUnitCount = 6;
queue.maxConcurrentOperationCount = 2;
NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"1:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"2:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"3:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation4 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"4:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation5 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"5:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
NSBlockOperation *blockOperation6 = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"6:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
[queue addOperation:blockOperation1];
[queue addOperation:blockOperation2];
[queue addOperation:blockOperation3];
[queue addOperation:blockOperation4];
[queue addOperation:blockOperation5];
[queue addOperation:blockOperation6];
[queue addBarrierBlock:^{
    NSLog(@"7:%@ progress:%@", [NSThread currentThread] ,queue.progress);
}];
/// 添加操作
- (void)addOperation:(NSOperation *)op;

/// 添加操作,wait:是否等操作完成
- (void)addOperations:(NSArray<NSOperation *> *)ops waitUntilFinished:(BOOL)wait API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 使用block的方式添加操作.
- (void)addOperationWithBlock:(void (^)(void))block API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 类似`dispatch_barrier_async`
- (void)addBarrierBlock:(void (^)(void))barrier API_AVAILABLE(macos(10.15), ios(13.0), tvos(13.0), watchos(6.0));

/// queue最大并发数(1的时候是串行线程)
@property NSInteger maxConcurrentOperationCount;

/// 暂停
@property (getter=isSuspended) BOOL suspended;

/// queue的名称
@property (nullable, copy) NSString *name API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 服务质量
@property NSQualityOfService qualityOfService API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

/// 任务执行的队列.
@property (nullable, assign /* actually retain */) dispatch_queue_t underlyingQueue API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

用例:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.underlyingQueue = dispatch_get_main_queue();
/// 取消queue上的所有操作.正在执行状态的操作
- (void)cancelAllOperations;

/// 等待所有任务执行完毕(会卡住创建queue的线程)
- (void)waitUntilAllOperationsAreFinished;

/// 获取当前queue
@property (class, readonly, strong, nullable) NSOperationQueue *currentQueue API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

/// 获取mainQueue(dispatch_get_main_queue())
@property (class, readonly, strong) NSOperationQueue *mainQueue API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));

Deprecated(已废弃)

// These two functions are inherently a race condition and should be avoided if possible

/// 所有操作(addBarrierBlock代替)
@property (readonly, copy) NSArray<__kindof NSOperation *> *operations API_DEPRECATED("access to operations is inherently a race condition, it should not be used. For barrier style behaviors please use addBarrierBlock: instead", macos(10.5, API_TO_BE_DEPRECATED), ios(2.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED));

/// 操作个数(使用queue.progress.completedUnitCount)
@property (readonly) NSUInteger operationCount API_DEPRECATED_WITH_REPLACEMENT("progress.completedUnitCount", macos(10.6, API_TO_BE_DEPRECATED), ios(4.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED));
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值