我想解析背景中的一些XML文件,以便UI不凍結。我必須檢查兩件事:如何檢查NSOperationQueue是否完成,如果有任何操作失敗?
- NSOperationQueue完成?
- NSOperation - 解析失敗了嗎?
我有一個類NSOperation的子類和一個代表被稱爲如果解析失敗。隊列中的操作同時被限制爲一個。
我的問題是,我不能依靠的事實,即在我得到隊列完成消息之前執行失敗的消息。有時我在收到完成的消息之前沒有收到失敗的消息。然後,例如,我有這樣的順序:
操作1個成功 操作2成功 OperationQueue完成 操作3失敗
所有消息都發送到主線程。在我收到完成的消息後,我想在我的代碼中進行操作,但前提是所有操作都成功。我如何處理隊列完成後調用委託消息的問題。
這是我的代碼的某些部分:
//XMLOperation.m
- (void)main {
NSLog(@"Operation started");
if ([self parseXML]) {
[self performSelectorOnMainThread:@selector(finishedXMLParsing) withObject:nil waitUntilDone:NO];
} else {
[self performSelectorOnMainThread:@selector(failedXMLParsing) withObject:nil waitUntilDone:NO];
}
}
NSLog(@"Operation finished");
}
//StartController.m
[self.xmlParseQueue addObserver:self forKeyPath:@"operations" options:0 context:NULL];
...
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (object == self.xmlParseQueue && [keyPath isEqualToString:@"operations"]) {
if ([self.xmlParseQueue.operations count] == 0) {
// Do something here when your queue has completed
NSLog(@"queue has completed");
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
}
爲2017年1月時,蘋果文檔中提到,您可以訪問 「operationCount」。只需在需要時檢查。 https://developer.apple.com/reference/foundation/nsoperationqueue?language=objc –