有一個WWDC視頻(2012年),可能會幫助你。它使用自定義的NSOperationQueue
並將異步塊放在NSOperations
內,以便您可以保留塊的句柄並取消剩餘的排隊塊。
一個想法是將錯誤處理的子塊調用處理NSOperationQueue
的類中的主線程上的方法。然後課堂可以適當地取消休息。這樣子塊只需要知道他們自己的線程和主線程。下面是視頻
https://developer.apple.com/videos/wwdc/2012/
視頻被稱爲「iOS上構建併發用戶界面」的鏈接。相關部分主要在下半年,但您可能希望看到整個事情,因爲它將它放在上下文中很好。
編輯:
如果可能的話,我建議你在處理的嵌入式模塊,它包裝好聽在一起,這是我認爲你後的反應..
//Define an NSBlockOperation, and get weak reference to it
NSBlockOperation *blockOp = [[NSBlockOperation alloc]init];
__weak NSBlockOperation *weakBlockOp = blockOp;
//Define the block and add to the NSOperationQueue, when the view controller is popped
//we can call -[NSOperationQueue cancelAllOperations] which will cancel all pending threaded ops
[blockOp addExecutionBlock: ^{
//Once a block is executing, will need to put manual checks to see if cancel flag has been set otherwise
//the operation will not be cancelled. The check is rather pointless in this example, but if the
//block contained multiple lines of long running code it would make sense to do this at safe points
if (![weakBlockOp isCancelled]) {
//substitute code in here, possibly use *synchronous* NSURLConnection to get
//what you need. This code will block the thread until the server response
//completes. Hence not executing the following block and keeping it on the
//queue.
__block NSData *temp;
response = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
[operationQueue addOperationWithBlock:^{
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Call selector on main thread to handle canceling
//Main thread can then use handle on NSOperationQueue
//to cancel the rest of the blocks
});
else {
//Continue executing relevant code....
}
}];
}
}];
[operationQueue addOperation:blockOp];
謝謝,我觀看了視頻。我想我所躊躇的是如何在等待異步響應的同時將操作基本上保存在隊列中?能夠利用NSOperationQueue會很方便。我之前在其他應用中使用過這個類,但之前我只有隊列處理出站請求 - 而不是響應處理。在這個應用程序中,操作不會完成,直到處理完響應並且任何關聯的子請求也完成。 – xyzzycoder 2012-08-09 02:42:22
你可以把響應處理代碼放入一個嵌入塊嗎?我會更新我的答案 – 2012-08-09 09:11:37
如果你在'NSOperation'世界,而不是'dispatch_async(dispatch_get_main_queue(),^ {});',爲什麼不''[[NSOperationQueue mainQueue] addOperationWithBlock:^ {}]; ?你得到的很好,但將GCD調用與「NSOperationQueue」調用混合起來感覺很奇怪。 – Rob 2013-01-02 22:28:37