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
,等...
但如果我有多个操作队列会发生什么?我应该检查每个操作队列吗?我可以做到这一点,但这是最好的办法吗? – 2011-03-07 08:54:22
@ xlc0212:我能想到的另一个解决方案是子类NSOperation,它实际上受到了Apple的鼓励;并添加一个布尔属性“isInQueue”,并将其添加到队列中时标记为YES。像这样,您只需在将操作添加到队列之前检查此属性。 – Zebs 2011-03-07 09:08:21
我应该何时标记操作?我应该在将它添加到队列中之前手动设置它,还是应该重写将由队列调用的一些方法,然后设置标志 – 2011-03-07 09:31:35