0

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?

4

2 回答 2

4

-connection:didReceiveResponse:将被调用 0 次或更多次。如果有错误,-connection:didFailWithError:将被调用。如果您收到多部分 mime 消息,则可能会多次调用此方法,并且一旦有足够的日期来创建响应对象并且调用之前,-connection:didReceiveData:就会调用此方法。

-connection:didReceiveData:将被调用 0 次或更多次。如果有超过 0 字节的主体,该方法将在被调用之前至少被调用一次-connection:didFinishLoading:。永远不会在 or 之前或之后调用此-connection:didReceiveResponse:方法。-connection:didFinishLoading:-connection:didFailWithError:

-connection:didFinishLoading:只调用一次,它是最后一次调用。此方法返回后的某个时间,连接将被释放。如果调用此方法,则不会调用此方法-connection:didFailWithError:,并且始终是最后调用的方法。

何时调用这些方法以及头文件中存在的顺序的文档,但我还没有看到它在实际文档中超级简洁地写出来。

于 2013-07-10T07:25:15.717 回答
2

对于委托方法,无论您按什么顺序放置它们。它们将在满足特定事件时发生。

第二件事,根据文档NSURLConnectionDataDelegate 协议参考

didReceiveResponse:当服务器确定它有足够的信息来创建 NSURLResponse 时调用此方法。它可以被多次调用,例如在重定向的情况下,所以每次我们重置数据。

didReceiveData:当连接中有新的可用数据时调用此方法。该方法被多次调用

connectionDidFinishLoading:连接成功完成加载时发送。在重定向的情况下,此方法也会多次调用。

于 2013-07-10T07:22:06.183 回答