取消NS操作
我在循环中使用以下代码:
-(void)getUpdatedComments
{
if(checkComments)
{
objParseOperation=[[ParseOperation alloc] initWithUDID:[[NSUserDefaults standardUserDefaults] valueForKey:kDeviceUDID]:self];
[operationQueue addOperation:objParseOperation3];
}
}
其中 operationQueue 是 NSOperationQueue 类型的对象。我每隔几秒调用一次这个方法。
如果我在第一次之后调用这个方法,我需要取消之前的操作还是只执行[objParseOperation release]
?
I am using the following code in a loop:
-(void)getUpdatedComments
{
if(checkComments)
{
objParseOperation=[[ParseOperation alloc] initWithUDID:[[NSUserDefaults standardUserDefaults] valueForKey:kDeviceUDID]:self];
[operationQueue addOperation:objParseOperation3];
}
}
where operationQueue is an object of type NSOperationQueue. I am calling this method every few seconds.
If I call this method after the first time, do I need to cancel the previous operation or just do [objParseOperation release]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您打算使用相同的队列来添加更多新操作,那么释放它并创建新操作就没有多大用处。只需取消所有操作然后添加更多操作就足够了。您可以通过调用 cancelAllOperations 在队列中。请注意,已经运行的操作将继续运行,除非它们检查取消。
释放队列是否会立即释放排队的操作没有记录,因此应被视为未定义的行为。因此,您不应该假设它会以任何方式工作,并且您不应该依赖它(请参阅自动释放 NSOperationQueue 有危险)。
然而,有证据表明操作将保留其队列,例如 GCD 队列在运行/挂起异步块时由系统保留。您可以阅读 Grand Central Dispatch 文档 “队列由系统保留,直到块运行完成”。尽管如此,文档并没有像上面提到的那样指定 NSOperationQueues 的行为。
If you are going to use the same queue for adding more new operations then there isn't much use in releasing it and creating a new one. Simply canceling all operations and then adding more operations is enough. You can do so by calling cancelAllOperations on the queue. Note that operations that are already running will continue to run unless they check for the cancellation.
Wether or not releasing the queue will immediately release the enqueued operations or not is not documented and hence should be considered as undefined behavior. Therefor you shouldn't assume that it will work either way and you should not rely on it (see dangerous to autorelease NSOperationQueue).
There are however evidence that suggests that operations will retain their queue such as that GCD queues are retained by the system while they have asynchronous blocks running/pending. You can read in the Grand Central Dispatch documentation that "the queue is retaind by the system until the block has run to completion". Still, the documentation doesn't specify the behavior for NSOperationQueues like mentioned above.