NSURLConnectionDataDelegate order of functions

陌路散爱 提交于 2019-12-11 20:51:42

问题


In the NSURLConnectionDataDelegate there are a couple of functions that are pretty essential to making sure everything worked but I'm never sure what happens when.

The functions...

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

and a couple of others.

Do they always happen in the order I've put them? i.e. is the response the first thing you get or can it happen any time in the life of the connection?


回答1:


-connection:didReceiveResponse: will be called 0 or more times. If there's an error, -connection:didFailWithError: will be called instead. This method may be called more than once if you're getting a multi-part mime message and will be called once there's enough date to create the response object and before -connection:didReceiveData: is called.

-connection:didReceiveData: will be called 0 or more times. If there is more than a 0 byte body, this method will be called at least once before -connection:didFinishLoading: is called. This method will never be called before -connection:didReceiveResponse: or after -connection:didFinishLoading: or -connection:didFailWithError:.

-connection:didFinishLoading: is called only once and it's the last thing called. Sometime after this method returns, the connection will be released. This method isn't called if -connection:didFailWithError: is called and is always the last thing called.

The documentation for when these methods are called and in which sequence exist in the header files, but I haven't seen it written up super concisely in the actual docs.




回答2:


For the delegate methods no matter in which order order do you put them . They will occur when the particular event is met .

The second thing, as per the document the NSURLConnectionDataDelegate Protocol Reference :

didReceiveResponse: This method is called when the server has determined that it has enough information to create the NSURLResponse. It can be called multiple times, for example in the case of a redirect, so each time we reset the data.

didReceiveData: This method is called when there is newly available data from the connection. This method is called multiple time

connectionDidFinishLoading:Sent when a connection has finished loading successfully. This method also called multiple time in case of redirect .



来源:https://stackoverflow.com/questions/17564435/nsurlconnectiondatadelegate-order-of-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!