Slinky
Slinky

Reputation: 5832

NSURLConnection Object Ignoring It's Implemented Methods

My question relates to the code in my single-threaded app that updates the devices' local database data via a NSURLConnection to a web service.

My NSURLConnection initialization is not going into any of it's implemented methods and it has me puzzled. Inside my AppDelegate.m -> applicationDidFinishLaunching method, I am creating a NSURLConnection object:

//AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application
{

   // ... Building request URL here ...

   NSString *requestURL = [NSString stringWithFormat:@"%@%@", URI,urlEncodedParamStr]
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]];
   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

   responseData = [[NSMutableData data] retain];

   if(connection !=nil)
      [connection release];

}

Execution should now proceed to any of the following appropriate NSURLConnection methods that I implemented in AppDelegate.m:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{[responseData setLength:0];}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {[responseData appendData:data];}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{//my implementation}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {//my implementation} 

But instead, execution continues, with the line directly after the init of NSURLConnection *connection, not executing any of the NSURLConnection implemented methods. I have confirmed that the request is not nil and put breakpoints in the implemented NSURLConnection methods - They are not being called.

Debugging session screenshot

What's going on?

Thanks as always!

Upvotes: 1

Views: 245

Answers (1)

bshirley
bshirley

Reputation: 8357

You need to call

[connection start];

before the remote call will actually be started.

And even then, the next line of code will execute. It's an asynchronous call and is executed in a background thread. The response is received later and your connection... method will be called then (which can be up to the timeout (30 seconds) time later).

Upvotes: 4

Related Questions