NSOperationQueue线程队列完毕finished状态检测
来源:互联网 发布:php 通过接口查询数据 编辑:程序博客网 时间:2024/06/04 18:29
参考:http://stackoverflow.com/questions/1049001/get-notification-when-nsoperationqueue-finishes-all-tasks
多线程编程中,操作队列NSOperationQueue我们经常会用到的,简化了多线程的操作。至于用法就不多介绍了。这里要说的是队列执行完毕的状态检查。
我们很多时候需要在队列完成之后再进行操作,而何时队列完成,NSOperationQueue并没有内置的didFinishedSelector来供使用,因此需要自己去检查其状态。
因为NSOperationQueue兼容 key-value coding (KVC) and key-value observing (KVO)机制,因此我们可以观察NSOperationQueue的属性。NSOperationQueue可供监控观察的属性有:
operations
- read-only propertyoperationCount
- read-only propertymaxConcurrentOperationCount
- readable and writable propertysuspended
- readable and writable propertyname
- readable and writable property
1.初始_parseQueue
- (NSOperationQueue *)parseQueue{ if (nil == _parseQueue) { _parseQueue = [[NSOperationQueue alloc] init]; [_parseQueue setSuspended:YES]; //[_parseQueue setMaxConcurrentOperationCount:1]; [_parseQueue addObserver:self forKeyPath:@"operations" options:0 context:nil]; } return _parseQueue;}
2.加入operation
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myTask) object:nil]; [self.parseQueue addOperation:operation]; [operation release];
3.观察值的改变:
//KVO,观察parseQueue是否执行完- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if (object == self.parseQueue && [keyPath isEqualToString:@"operations"]) { if (0 == self.parseQueue.operations.count) { DLog(@"parse finished"); //other operation [_parseQueue setSuspended:YES]; } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; }}
选择对NSOperationQueue的operations进行观察,而不选operationCount是因为operationCount需要iOS 4.0以上。
- NSOperationQueue线程队列完毕finished状态检测
- NSOperationQueue线程队列完毕finished状态检测
- 多线程之NSOperationQueue(线程队列)
- NSOperationQueue 队列
- NSOperationQueue队列
- iPhone多线程开发之线程队列NSOperationQueue与自定义Protocol
- NSOperationQueue 简化线程开发 和 队列的优先级
- iOS多线程_获取线程队列执行完毕的信号
- iPhone 操作队列NSOperationQueue
- NSOperationQueue队列的优先级
- iPhone 操作队列NSOperationQueue
- NSOperationQueue队列的特点
- NSOperationQueue(多线程请求队列)
- 使用NSOperationQueue起线程
- NSOperationQueue 方式开启线程
- 线程池NSOperationQueue
- GCD NSOperationQueue线程依赖
- NSOperationQueue 创建线程
- 从SQL语句的角度提高数据库性能
- Linux 7z,tar解压缩 使用
- mysql分析工具
- 李彦宏:黑马是怎样炼成的
- Page cache和buffer cache的区别与联系(ZT)
- NSOperationQueue线程队列完毕finished状态检测
- Android之路-生命周期
- PageRank算法
- 程序员们 不要想一辈子靠技术混饭吃
- 关于oracle with table as的用法
- 落户csdn
- 网络编程之小小示例
- 电子工程师的百变人生(上)
- 电子工程师的百变人生(下)