2011-09-09 23 views
2

背景:在握手過程中,我的iOS應用學習通過didReceiveResponse是否保證位於connectionDidFinishLoading之前?

  1. 服務器功能檢查它的頭信息didReceiveResponse

  2. 分析其響應XML中connectionDidFinishLoading

問題說明:由於回撥#1在#2之前,因此我將版本字符串存儲在didReceiveResponse中,並在響應可用時稍後在connectionDidFinishLoading中檢查它。

這個幸運的正常工作,到目前爲止,因爲#1 preceeds 2#。但是,這個訂單總是由網絡/ iOS保證的嗎?

回答

2

根據文檔:

Zero or more connection:didReceiveResponse: messages will be sent to the delegate before receiving a connection:didReceiveData: message. The only case where connection:didReceiveResponse: is not sent to a delegate is when the protocol implementation encounters an error before a response could be created. Zero or more connection:didReceiveData: messages will be sent before any of the following messages are sent to the delegate: connection:willCacheResponse:, connectionDidFinishLoading:, connection:didFailWithError:.

所以,除非有一個錯誤,你能保證你會得到一個connectionDidFinishLoading之前得到didReceiveResponse。

+1

這可能是一個跛腳的問題,但什麼構成的響應被創建之前的錯誤:在當所述協議實現遇到錯誤可以創建一個響應之前 – cynistersix

0

是的,當然。在didReceiveResponse你可以檢查NSURLResponse,如果有什麼不對,你可以停止下載。因此connectionDidFinishLoading只會在您檢查回覆後纔會被調用。

3

NSURLCOnnection Reference

Zero or more connection:didReceiveResponse: messages will be sent 

to the delegate before receiving a connection:didReceiveData: message. The only case where connection:didReceiveResponse: is not sent to a delegate is when the protocol implementation encounters an error before a response could be created.

Zero or more connection:didReceiveData: messages will be sent 

before any of the following messages are sent to the delegate: connection:willCacheResponse:, connectionDidFinishLoading:, connection:didFailWithError:.

Zero or one connection:willCacheResponse: messages will be sent to 

the delegate after connection:didReceiveData: is sent but before a connectionDidFinishLoading: message is sent.

所以,它不僅會如果創建響應之前的錯誤發生。

+0

>的唯一情況,其中該消息不被髮送到委託爲什麼你重複@Malcolm Box答案? – Nekto

+1

我很感謝您關注Nekto,但我並沒有故意這麼做。我想我們會在相似的時間發佈答案。 – Madhu

+0

我看到你在2分鐘後發佈你的答案。所以... – Nekto

相關問題