gpt4 book ai didi

iphone - iOS - 如何检查 NSOperation 是否在 NSOperationQueue 中?

转载 作者:可可西里 更新时间:2023-11-01 03:36:36 24 4
gpt4 key购买 nike

来自文档:

An operation object can be in at most one operation queue at a time and this method throws an NSInvalidArgumentException exception if the operation is already in another queue. Similarly, this method throws an NSInvalidArgumentException exception if the operation is currently executing or has already finished executing.

那么如何检查我是否可以安全地将 NSOperation 添加到队列中?

我知道的唯一方法是添加操作,然后在操作已经在队列中或之前执行时 try catch 异常。

最佳答案

NSOperationQueue 对象有一个名为 operations 的属性。

如果您有对队列的引用,则很容易检查。

您可以像这样检查 NSArray 操作是否包含您的 NSOperation:

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");
}

告诉我这是否是您要找的。

或者,NSOperation 对象有几个允许您检查它们状态的属性;比如:isExecuting, isFinished, isCancelled, etc...

关于iphone - iOS - 如何检查 NSOperation 是否在 NSOperationQueue 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5217504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com