NSOperation KVO已完成 [英] NSOperation KVO isFinished

查看:192
本文介绍了NSOperation KVO已完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对NSOperation进行子类化,并从中读取一些示例, 他们说:当任务完成时,使用NSOperation的KVO完成操作, 代码在这里:

Im trying to subclass a NSOperation, and read some sample from, they say: when the task finished, using KVO of NSOperation, to finish the operation, code here:

[self willChangeValueForKey:@"isFinished"];
[self willChangeValueForKey:@"isExecuting"]
finished = YES;
executing = NO;
[self didChangeValueForKey:@"isFinished"];
[self didChangeValueForKey:@"isExecuting"];

然后isFinished被调用

then isFinished get called

- (BOOL) isFinished{
    return(finished);
}

有人可以向我解释吗?为什么isFinished被调用,isFinished会完成操作吗?据我了解,KVO是否需要手动[self didChangeValueForKey:@"isExecuting"];而且我没有看到像addobserver:和observeValueForKeyPath这样的代码:

anyone could explain this to me? why isFinished gets called, will the isFinished finish the operation? as I understanded, do KVO manually need [self didChangeValueForKey:@"isExecuting"]; and I didnt see code like addobserver: and observeValueForKeyPath:

我写

 -(void)call
{
     [self willChangeValueForKey:@"isVip"];
     [self didChangeValueForKey:@"isVip"];
}

-(void)isVip
{
    NSLog(@"Im vip");
}

在做[自我呼叫]时不调用isVip;

isVip is not called when do [self call];

推荐答案

NSOperationQueue实现将观察您的操作的"isFinished"属性(使用KVO),因此它知道何时将其从队列中删除.被告知isFinished值更改后,很可能被Apple内部代码调用.

The NSOperationQueue implementation will observe the "isFinished" property of your operation (using KVO) so it knows when to remove it from the queue. isFinished is most likely being called by internal Apple code after it has been told of the change to its value.

这篇关于NSOperation KVO已完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆