每当从我的 NSOperationQueue 中添加或删除 NSOperation 时,我都希望收到通知。我正在尝试为“操作”属性(当前在队列中的 NSOperations 数组)设置键值观察,但它没有被触发。我的语法有问题吗?
@implementation myOperationQueueSubclass
-(id)init
{
if (self = [super init])
{
// Initialization code here
[self addObserver:self
forKeyPath:@"operations"
options:0
context:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"queue changed...");
if ([keyPath isEqualToString:@"operations"]) {
if (self.operationCount == 0) {
// No ops
} else {
// Has ops
}
}
}