2012-10-28 84 views
1

我的情況,我必須遍歷賬號列表,去獲取這些帳戶的詳細信息,然後通過彙總信息completionBlock被調用。爲了促進這一點,我使用dispatch_group_ *方法。不幸的是,在完成塊被稱爲之前,該集團已經完成了,即使我已經明確調用控制檯日誌的我如何確保調度通知在正確的時間

dispatch_group_wait(_imfDispatchGroup, DISPATCH_TIME_FOREVER);

例(原件):

2012-10-28 13:58:46.722 ThreadingHell[81909:303] inside loginResponse

2012-10-28 13:58:46.724 ThreadingHell[81909:303] inside account loop for obj: account1

2012-10-28 13:58:46.724 ThreadingHell[81909:303] inside account loop for obj: account2

2012-10-28 13:58:46.724 ThreadingHell[81909:1b03] inside details: L0i6kRVzGG details

2012-10-28 13:58:46.724 ThreadingHell[81909:1a03] inside details: SE0QjhO8Hh details

2012-10-28 13:58:46.724 ThreadingHell[81909:303] inside account loop for obj: account3

2012-10-28 13:58:46.725 ThreadingHell[81909:1a03] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_015

2012-10-28 13:58:46.725 ThreadingHell[81909:1803] inside details: 9SsGYOX3u5 details

2012-10-28 13:58:46.725 ThreadingHell[81909:1a03] details are found for account

2012-10-28 13:58:46.725 ThreadingHell[81909:303] inside account loop for obj: account4

2012-10-28 13:58:46.725 ThreadingHell[81909:1b03] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_015

2012-10-28 13:58:46.726 ThreadingHell[81909:1803] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_015

2012-10-28 13:58:46.726 ThreadingHell[81909:303] inside account loop for obj: acount5

2012-10-28 13:58:46.726 ThreadingHell[81909:1a03] success account looop

2012-10-28 13:58:46.726 ThreadingHell[81909:1b03] details are found for account

2012-10-28 13:58:46.726 ThreadingHell[81909:2303] inside details: wSXis5bIVs details

2012-10-28 13:58:46.728 ThreadingHell[81909:1803] details are found for account

2012-10-28 13:58:46.728 ThreadingHell[81909:2403] inside details: SrIfbzBLr7 details

2012-10-28 13:58:46.728 ThreadingHell[81909:1a03] current instance of accountDetails: ( "L0i6kRVzGG details")

2012-10-28 13:58:46.728 ThreadingHell[81909:1803] success account looop

2012-10-28 13:58:46.728 ThreadingHell[81909:1b03] success account looop

2012-10-28 13:58:46.729 ThreadingHell[81909:2403] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_015

2012-10-28 13:58:46.729 ThreadingHell[81909:303] !!!!!!!!!! about to call completion block. This should be last

2012-10-28 13:58:46.729 ThreadingHell[81909:2303] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_015

2012-10-28 13:58:46.730 ThreadingHell[81909:1803] current instance of accountDetails: ( "L0i6kRVzGG details", "9SsGYOX3u5 details")

2012-10-28 13:58:46.730 ThreadingHell[81909:1b03] current instance of accountDetails: ( "L0i6kRVzGG details", "9SsGYOX3u5 details", "SE0QjhO8Hh details")

2012-10-28 13:58:46.731 ThreadingHell[81909:2403] details are found for account

2012-10-28 13:58:46.731 ThreadingHell[81909:2303] details are found for account

2012-10-28 13:58:46.732 ThreadingHell[81909:2403] success account looop

2012-10-28 13:58:46.731 ThreadingHell[81909:303] obj: { }

2012-10-28 13:58:46.732 ThreadingHell[81909:2303] success account looop

2012-10-28 13:58:46.732 ThreadingHell[81909:303] error: (null)

2012-10-28 13:58:46.732 ThreadingHell[81909:2403] current instance of accountDetails: ( "L0i6kRVzGG details", "9SsGYOX3u5 details", "SE0QjhO8Hh details", "wSXis5bIVs details")

//修訂從LOG最新提交[https://github.com/kwylez/ThreadingHell/tree/80cc29fab142b4dc1f386df747c5142b28e2dd84]

2012-10-28 16:32:38.984 ThreadingHell[83171:303] inside loginResponse 2012-10-28 16:32:38.987 ThreadingHell[83171:303] inside account loop for obj: account1 2012-10-28 16:32:38.987 ThreadingHell[83171:303] inside account loop for obj: account2 2012-10-28 16:32:38.987 ThreadingHell[83171:1b03] inside details: CP3ioYdvXp details 2012-10-28 16:32:38.987 ThreadingHell[83171:303] inside account loop for obj: account3 2012-10-28 16:32:38.987 ThreadingHell[83171:1a03] inside details: 6k635XoJOV details 2012-10-28 16:32:38.987 ThreadingHell[83171:1b03] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_0 2012-10-28 16:32:38.988 ThreadingHell[83171:1a03] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_0 2012-10-28 16:32:38.988 ThreadingHell[83171:1b03] details are found for account 2012-10-28 16:32:38.988 ThreadingHell[83171:303] inside account loop for obj: account4 2012-10-28 16:32:38.988 ThreadingHell[83171:1803] inside details: 2TjGF1fdaZ details 2012-10-28 16:32:38.989 ThreadingHell[83171:1a03] details are found for account 2012-10-28 16:32:38.990 ThreadingHell[83171:1b03] success account looop 2012-10-28 16:32:38.991 ThreadingHell[83171:1b03] current instance of accountDetails: ( "CP3ioYdvXp details") 2012-10-28 16:32:38.990 ThreadingHell[83171:303] inside account loop for obj: acount5 2012-10-28 16:32:38.990 ThreadingHell[83171:2303] inside details: 0YiuX9gHsu details 2012-10-28 16:32:38.990 ThreadingHell[83171:1a03] success account looop 2012-10-28 16:32:38.990 ThreadingHell[83171:1803] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_0 2012-10-28 16:32:38.992 ThreadingHell[83171:2303] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_0 2012-10-28 16:32:38.992 ThreadingHell[83171:1b03] inside details: 9RcyOfjRa5 details 2012-10-28 16:32:38.992 ThreadingHell[83171:1a03] current instance of accountDetails: ( "CP3ioYdvXp details", "6k635XoJOV details") 2012-10-28 16:32:38.992 ThreadingHell[83171:1803] details are found for account 2012-10-28 16:32:39.005 ThreadingHell[83171:1803] success account looop 2012-10-28 16:32:39.005 ThreadingHell[83171:1b03] inside completion for __66-[ModelManager getAccountDetailsForAccountNumber:completionBlock:]_block_invoke_0 2012-10-28 16:32:39.005 ThreadingHell[83171:2303] details are found for account 2012-10-28 16:32:39.006 ThreadingHell[83171:1b03] details are found for account 2012-10-28 16:32:39.006 ThreadingHell[83171:2303] success account looop 2012-10-28 16:32:39.006 ThreadingHell[83171:1803] current instance of accountDetails: ( "CP3ioYdvXp details", "6k635XoJOV details", "2TjGF1fdaZ details") 2012-10-28 16:32:39.007 ThreadingHell[83171:1b03] success account looop 2012-10-28 16:32:39.007 ThreadingHell[83171:2303] current instance of accountDetails: ( "CP3ioYdvXp details", "6k635XoJOV details", "2TjGF1fdaZ details", "0YiuX9gHsu details") 2012-10-28 16:32:39.008 ThreadingHell[83171:1b03] current instance of accountDetails: ( "CP3ioYdvXp details", "6k635XoJOV details", "2TjGF1fdaZ details", "0YiuX9gHsu details", "9RcyOfjRa5 details") 2012-10-28 16:32:39.008 ThreadingHell[83171:303] !!!!!!!!!! about to call completion block. This should be last 2012-10-28 16:32:39.009 ThreadingHell[83171:303] obj: { } 2012-10-28 16:32:39.009 ThreadingHell[83171:303] error: (null)

我錯過了什麼「操作順序」?

這裏是鏈接到示例項目在GitHub上(代碼是太多,放在這裏):

https://github.com/kwylez/ThreadingHell

+0

+1爲項目名稱。 – jrturton

+0

我更新了項目,並且已經接近了一些。所有「塊」以正確的順序進行,但最終數組在返回最終完成塊時爲空。 我編輯了新的日誌原來的問題比較 –

回答

0

(我還沒有下載你的項目呢,回答我的手機上,道歉.. 。)

使用NSOperationQueue代替。將每個操作添加爲完成操作的依賴項。

這是一個更清潔,更類似可可的API。你不應該改變太多,因爲你可以使用NSBlockOperation並傳入你當前用於GCD的相同塊。

+0

我可能會走這條路,但我覺得我接近與GCD的解決方案,並希望看到它通過......可能有一個NSOperationQueue和GCD的解決方案。 –

0

最新的代碼commit

修復去年我提出的意見的問題,因此,我的問題是使用dispatch_group方法回答...。

getAccountDetailsForAccountNumber:completionBlock方法中,我刪除了對dispatch_group_notify的調用,並在dispatch_group_async方法中移動了「完成」塊登錄。這解決了問題,因爲完成塊現在是隊列任務的一部分,並且getAccountListMetaInformation:將不得不「遵守」dispatch_group_wait命令,而不是之前通知將接收到併發通知之前。

+0

您能否給出您所做更改的摘要,因爲這個問題在沒有鏈接項目的情況下是無用的,我們不喜歡這裏的外部依賴項! – jrturton

+0

今天我會補充一點 –