NSOperation が NSOperationQueue に追加または削除されたときに通知を受け取りたいです。「操作」プロパティ (現在キューにある 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
}
}
}