NSOperation and KVO/KVC coding

| No Comments | No TrackBacks

本文简要介绍如何使用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 方法

KeyPath 官方文档说明 http://developer.apple.com/mac/library/documentation/cocoa/reference/NSOperation_class/Reference/Reference.html#//apple_ref/doc/c_ref/NSOperation

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 property

  • isConcurrent - read-only property

  • isExecuting - read-only property

  • isFinished - read-only property

  • isReady - read-only property

  • dependencies - read-only property

  • queuePriority - readable and writable property

  • completionBlock - 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];

}


#pragma mark -

#pragma mark KVO 回调方法

- (void)observeValueForKeyPath:(NSString *)keyPath

                      ofObject:(id)object

                        change:(NSDictionary *)change

                       context:(void *)context

{

NSLog(@"dict= %@ obj= %@,keypath=%@",change,object,keyPath);

}

NSOperation 完成后回调方法,执行完第一个下载,cancel所有

- (void)pageLoaded:(NSString*)document;

{

NSLog(@"%s Do something with the XMLDocument", _cmd);

for (id opt in [queue operations]) {

NSLog(@"opt:%@ will cancel",opt);

[(NSOperation*)opt cancel];

   

}

    

}


执行log如下

[Session started at 2010-05-25 13:31:26 +0800.]

2010-05-25 13:31:28.030 AsyncDownloader[2672:4503] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3b0a530>,keypath=isExecuting

2010-05-25 13:31:28.032 AsyncDownloader[2672:4903] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x390dc00>,keypath=isExecuting

2010-05-25 13:31:28.490 AsyncDownloader[2672:207] pageLoaded: Do something with the XMLDocument

2010-05-25 13:31:28.490 AsyncDownloader[2672:4903] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x390dc00>,keypath=isExecuting

2010-05-25 13:31:28.490 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3b0a530> will cancel

2010-05-25 13:31:28.494 AsyncDownloader[2672:207] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3b0a530>,keypath=isCancelled

2010-05-25 13:31:28.495 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x390dc00> will cancel

2010-05-25 13:31:28.495 AsyncDownloader[2672:4903] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x390dc00>,keypath=isFinished

2010-05-25 13:31:28.495 AsyncDownloader[2672:6607] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3912ae0>,keypath=isExecuting

2010-05-25 13:31:28.496 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3912ae0> will cancel

2010-05-25 13:31:28.496 AsyncDownloader[2672:207] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3912ae0>,keypath=isCancelled

2010-05-25 13:31:28.497 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3913470> will cancel

2010-05-25 13:31:28.497 AsyncDownloader[2672:207] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3913470>,keypath=isCancelled

2010-05-25 13:31:28.498 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x39135f0> will cancel

2010-05-25 13:31:28.499 AsyncDownloader[2672:207] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x39135f0>,keypath=isCancelled

2010-05-25 13:31:28.916 AsyncDownloader[2672:207] pageLoaded: Do something with the XMLDocument

2010-05-25 13:31:28.916 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3b0a530> will cancel

2010-05-25 13:31:28.916 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3912ae0> will cancel

2010-05-25 13:31:28.916 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3913470> will cancel

2010-05-25 13:31:28.916 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x39135f0> will cancel

2010-05-25 13:31:28.916 AsyncDownloader[2672:4503] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3b0a530>,keypath=isExecuting

2010-05-25 13:31:28.918 AsyncDownloader[2672:4503] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3b0a530>,keypath=isFinished

2010-05-25 13:31:28.920 AsyncDownloader[2672:5a33] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3913470>,keypath=isFinished

2010-05-25 13:31:28.922 AsyncDownloader[2672:4507] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x39135f0>,keypath=isFinished

2010-05-25 13:31:30.652 AsyncDownloader[2672:6607] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3912ae0>,keypath=isExecuting

2010-05-25 13:31:30.652 AsyncDownloader[2672:207] pageLoaded: Do something with the XMLDocument

2010-05-25 13:31:30.653 AsyncDownloader[2672:207] opt:<PageLoadOperation: 0x3912ae0> will cancel

2010-05-25 13:31:30.654 AsyncDownloader[2672:6607] dict= {

    kind = 1;

} obj= <PageLoadOperation: 0x3912ae0>,keypath=isFinished


测试项目代码下载 AsyncDownloader.zip


No TrackBacks

TrackBack URL: http://iphone.ipsw.info/mt/mt-tb.cgi/305

Leave a comment