本文简要介绍如何使用KVO 跟踪NSOperation 状态
traceOperation 方法跟踪 PageLoadOperation(NSOperation 子类)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
for (NSString *urlString in urlArray)
{
NSURL *url = [NSURL URLWithString:urlString];
PageLoadOperation *plo = [[PageLoadOperation alloc] initWithURL:url];
[queue addOperation:plo];
[self traceOperation:plo];
[plo release];
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
traceOperation 方法
KVO-Compliant Properties
The NSOperation class is key-value coding (KVC) and key-value observing (KVO) compliant for several of its properties. As needed, you can observe these properties to control other parts of your application. The properties you can observe include the following:
isCancelled- read-only propertyisConcurrent- read-only propertyisExecuting- read-only propertyisFinished- read-only propertyisReady- read-only propertydependencies- read-only propertyqueuePriority- readable and writable propertycompletionBlock- readable and writable property (Mac OS X only)
-(void)traceOperation:(NSOperation*)obj
{
[obj addObserver:self
forKeyPath:@"isExecuting"
options:0
context:NULL];
[obj addObserver:self
forKeyPath:@"isFinished"
options:0
context:NULL];
[obj addObserver:self
forKeyPath:@"isReady"
options:0
context:NULL];
[obj addObserver:self
forKeyPath:@"isCancelled"
options:0
context:NULL];
}
