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