Bry*_*hen 6 iphone objective-c nsoperation nsoperationqueue ios
来自文档:
操作对象一次最多只能有一个操作队列,如果操作已经在另一个队列中,则此方法抛出NSInvalidArgumentException异常.同样,如果操作当前正在执行或已经完成执行,则此方法抛出NSInvalidArgumentException异常.
那么如何检查我是否可以安全地将NSOperation添加到队列中?
我知道的唯一方法是添加操作,然后尝试捕获异常,如果操作已经在队列中或之前执行过.
Zeb*_*ebs 16
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");
Run Code Online (Sandbox Code Playgroud)
或者您可以迭代所有对象:
for(NSOperation *op in [queue operations])
if (op==operation) {
NSLog(@"Operation is in the queue");
}
else {
NSLog(@"Operation is not in the queue");
}
Run Code Online (Sandbox Code Playgroud)
告诉我这是否是你要找的.
或者,NSOperation对象具有多个允许您检查其状态的属性; 如:isExecuting,isFinished,isCancelled,等...