NSOperation and KVO/KVC coding

NSOperation and KVO/KVC coding

|  No Comments  |  No TrackBacks
原文地址: http://iphone.ipsw.info/2010/05/nsoperation-and-kvokvc-coding.html

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值