我在SO上遵循了许多链接,为队列应用了方法,但似乎observeValueForKeyPath:每次队列添加操作时都不会调用。
- (id)init {
if(!(self = [super init])) return nil;
_imageQueue = [[NSOperationQueue alloc] init];
[_imageQueue addObserver:self forKeyPath:@"imageQueue" options:0 context:nil];
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"Observer Received");
}
- (void)loadPreviewPageWithMagazineID:(NSString *)magazineID userID:(NSString *)userID {
[_imageQueue setMaxConcurrentOperationCount:2];
for (NSInteger i = 1; i <= _numTotalPages; ++i) {
//NSLog(@"currenpage = %d, index = %d",_selectedPage,pageIndex);
NSDictionary *arguments = [NSDictionary dictionaryWithObjectsAndKeys:magazineID,
@"magazineID", userID, @"userID", [NSNumber numberWithInt:i],
@"pageNumber", nil];
NSInvocationOperation *operation =
[[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(savePageToDisk:)
object:arguments];
[_imageQueue addOperation:operation];
[operation release];
}
}
我在另一个队列中调用了loadPreviewPage函数。显然调用了该函数,但是为什么没有调用observeValueForKeyPath?