iPhone-从NSOperation返回 [英] iPhone - return from an NSOperation

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

问题描述

我正在使用NSOperation的子类来执行一些后台进程.我希望用户单击按钮时取消该操作.

I am using a subclass of NSOperation to do some background processes. I want the operation to be cancelled when the user clicks a button.

这是我的NSOperation子类的样子

Here's what my NSOperation subclass looks like

- (id)init{
    self = [super init];
    if(self){
        //initialization code goes here
        _isFinished = NO;
        _isExecuting = NO;
    }

    return self;
}

- (void)start
{
    if (![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
        return;
    }
    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];

    //operation goes here
}

- (void)finish{
    //releasing objects here

    [self willChangeValueForKey:@"isExecuting"];
    [self willChangeValueForKey:@"isFinished"];

    _isExecuting = NO;
    _isFinished = YES;

    [self didChangeValueForKey:@"isExecuting"];
    [self didChangeValueForKey:@"isFinished"];
}

- (void)cancel{
    [self willChangeValueForKey:@"isCancelled"];

    [self didChangeValueForKey:@"isCancelled"];
    [self finish];
}

这就是我将此类的对象添加到队列中并侦听KVO通知的方式

And this is how I am adding objects of this class to a queue and listening for KVO notifications

operationQueue = [[NSOperationQueue alloc] init]; [operationQueue setMaxConcurrentOperationCount:5]; [operationQueue addObserver:self forKeyPath:@"operations"选项:0上下文:& OperationsChangedContext];

operationQueue = [[NSOperationQueue alloc] init]; [operationQueue setMaxConcurrentOperationCount:5]; [operationQueue addObserver:self forKeyPath:@"operations" options:0 context:&OperationsChangedContext];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == &OperationsChangedContext) {
        NSLog(@"Queue size: %u", [[operationQueue operations] count]);
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

要取消操作(例如在按钮上单击),我尝试调用-cancel,但这没有什么不同.还尝试调用-finish,但即使那样也没有任何改变.

To cancel an operation (on a button click for instance), I tried calling -cancel but it doesn't make a difference. Also tried calling -finish but even that doesn't change anything.

每次我向队列添加操作时,队列大小只会增加.完成被调用(使用NSLog语句检查),但是它并没有真正结束操作.我仍然不太确定自己做对了

Every time I add an operation to the queue, the queue size only increases. finish is called (checked using NSLog statements) but it doesn't really end the operation. I'm still not very confident I'm doing this right

有人可以告诉我我要去哪里错吗?

Can someone please tell me where I am going wrong?

非常感谢

推荐答案

来自

取消操作不会立即强制其停止正在执行的操作.尽管尊重所有操作均应遵循isCancelled返回的值,但您的代码必须显式检查此方法返回的值并根据需要中止.

即除非您的实现强制执行,否则cancel方法实际上不会取消该操作.同样,从相关的部分:

i.e. the cancel method does not actually cancel the operation unless your implementation enforces this. Again, from the relevant section:

- (void)cancel

此方法不会强制您的操作代码停止.相反,它会更新对象的内部标志以反映状态的变化.

This method does not force your operation code to stop. Instead, it updates the object’s internal flags to reflect the change in state.

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

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