NSURLConnection Delegate - how does it know which methods?


#1

I am having a bit of trouble wrapping my head around the construct for delegates and associated protocol methods:

The exercise picks the following three methods from the NSURLConnection protocols for this delegate

[code]- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection
  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error[/code]

However, unlike target-action (where selectors are specifically specified in the construct), I do not see how/where the above three methods are being called. If the delegate itself takes over (after being hande the NSURLConnection arguments from ‘main’, how does it know that these are the three specific methods you chose from the Protocols (as there are several)? What is the flow behind the scenes during execution? In addition, the way each of these three methods are implemented in relation to their declarations is a bit confusing. The declarations all have arguments not necessarily in use by the implementations given in this example.

For instance, this method is supposed to be invoked as data starts to load? who and when calls it? Also, why does it log bytes received before building a Mutable data object? How is the [color=#008040](NSData *)data [/color]argument worked here if the NSMutableData object doesn’t even exist yet? The same very much goes for the remaining methods. I really wish this chapter had gone into more detail here.

[code]/* called each time a chunk of data arrives */

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    NSLog(@“received %lu bytes”, [data length]);

    // create a mutable data if it does not already exist
    if (!_incomingData) {
    _incomingData = [[NSMutableData alloc] init];
    }

    [_incomingData appendData:data];
    }[/code]


#2

The NSURLConnection object calls those methods on its delegate. The connection object can find out if the delegate has implemented those methods and invoke them when needed. You provide the connection object with a delegate when you load the data asynchronously.


#3

I read the framework documentation, and so I see these protocol methods are declared as @optional so, if I understand correctly, the NSURLConnection object is going to check the delegate object for an implementation on each of the protocol method declarations and if one is found to be implemented, that’s how it knows to process it?

if ([self.dataSource respondsToSelector:@selector(nameOfMethod)]) { // action to take // }

So, would it be safe to say that the Run Loop is constantly processing imput from the NSURLConnection instance which is in turn sending these messages to the delegate everytime a new chunk of data gets downloaded OR is the delegate tracking what gets downloaded (or merely providing feedback/calling back as per the methods it implemented? The reason I am having trouble with this is I understood the purpose of a delegate/helper was to avoid having the main thread held up processing a given function (offloading to the delegate) but it seems as though the NSURLConnection instance interacts with its delegate throughout the entire loop thread? hope that makes sense…

developer.apple.com/library/mac … 0-CH11-SW6


#4

I think I’ve answered my own questions after adding another delegate method and observing the behavior

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSLog(@"Did get a response from %@", response.URL); }

–output–
[color=#0000FF]2014-11-18 17:19:23.328 ObjC_Callbacks[2370:773695] Did get a response from gutenberg.org/cache/epub/205/pg205.txt[/color]