A little clarification


#1

I was just going back through the Callbacks chapter for deeper understanding and stumbled upon something that I’m missing fundamentally. I get why you need to declare variables and that you create a pointer and create an object to put there.

This makes sense:

Mostly because I understand allocating some memory for variables and methods in an object.

But looking at this got me thinking:

__unused NSURLConnection *fetchConn = [[NSURLConnection alloc] initWithRequest:request delegate:logger startImmediately:YES];
What makes the NSURLConnection object “do” what it does? It this just a function of all NSURLConnection objects? Do they, upon creation on the heap, just start doing what you ask? If so, is the following statement true about the code above: “A NSURLConnection pointer is declared then an NSURLConnection instance is created on the heap then upon initialization starts executing outside the main loop/thread.” (Be gentle, I’m not up on my thread understanding yet.)

I’m hoping that makes sense. Any clarifications would be welcome.


#2

This statement is correct: “They, upon creation on the heap, just start doing what you ask.”

The mechanism by which NSURLConnection does its job is a little deeper (whether it spawns a new thread or whether it just registers a socket with the run loop), but the good part is this: You don’t need to understand the mechanism. In fact, I think Apple has changed the exact implementation of this method a couple of times. You just need to know that as the delegate you will get callbacks when the data arrives.


#3

Thank you Aaron.