2014-06-17 44 views
2

我在我目前的应用程序中继承NSOperation。所有功能都按照我的愿望运行良好。我可以取消操作并将新操作添加到我的操作队列中。除了成功,下面是警告/错误的语句我在控制台中发现:NSOPeration取消错误

***SubOperation 0x19ec1bd0 went isFinished=YES without being started by the queue it is in* 

通过上面的警告,我能够发现,我开始前取消操作。因此,未来是否会发生内存泄漏或功能中断?

警告的意图究竟是什么?

到目前为止,功能正在发挥作用,并且没有任何突破,即使我们仍然需要考虑警告并解决它吗?

所有的建议都是开放的。

+1

这将是很大的帮助完整,如果有人给出了投票的理由... – ajay

回答

0

然后检查它第一次操作是在队列或不

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

NSOperation *operation = [[NSOperation alloc] init]; 

[queue addOperation:operation]; 

if([queue operations] containsObject:operation]) 
    NSLog(@"Operation is in the queue"); 
else 
    NSLog(@"Operation is not in the queue"); 

或者你可以遍历所有的对象:

for(NSOperation *op in [queue operations]) 
    if (op==operation) { 
     NSLog(@"Operation is in the queue"); 
    } 
    else { 
     NSLog(@"Operation is not in the queue"); 
    } 
+1

感谢您的快速回复。如果我没有开始取消,会有什么影响吗? – ajay

1

您有责任定期检查[self isCancelled]同时继承NSOperation如果是YES,则退出该操作。操作队列不能(立即)取消已经运行的操作。

如果覆盖ready,则也必须覆盖cancel。抽象类中发生的情况是,当cancel被调用时,它将操作设置为ready,以便队列可以调用start,方法start检查取消标志,然后中止操作并设置isFinished=YES。然后操作队列dealloc的操作。没有其他人就没有一个。

+1

不错的复制和粘贴从http://stackoverflow.com/a/12942342/1757581 – hacker2007