NSURLConnection not starting
I am trying to create a very simple application that connects to a URL and gets the content of that URL, in this case it is a simple XML document. My problem is that the request is never sent.
I created a new project (Foundation tool) that runs from the main file. I have set up delegate NSURLConnection for myself and implement the required delegation methods. The problem is that these methods are never called. It doesn't matter if I change the url to some dummy one (like in the example below). It looks to me like the app finishes launching without sending or waiting for a response. Maybe my application creates a thread for the request and then ends without waiting for a response? I know the class is running since I am receiving NSLOG statements of type "Created Connection", but no NSLOG statements from delegate methods.
What am I missing?
#import "NetworkController.h"
@implementation NetworkController
- (id)init
{
if(self = [super init])
{
}
return self;
}
- (void)dealloc
{
NSLog(@"Calling dealloc");
[super dealloc];
}
- (void) performConnection
{
NSMutableData *receivedData;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
@"http://www.test.php?some variables"]];
NSMutableURLRequest *connectionRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[url release];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:connectionRequest delegate:self startImmediately:YES];
if (!theConnection)
{
NSLog(@"Failed to submit request");
}
if(theConnection)
{
receivedData=[[NSMutableData data] retain];
NSLog(@"Created connection.");
NSLog(@"--------- Request submitted ---------");
NSLog(@"receivedData: %@", receivedData);
}
[connectionRequest release];
}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Received response: %@", response);
NSLog(@"Received response, connection retain count is %d",[connection retainCount]);
}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"Connection received data, retain count: %d", [connection retainCount]);
}
#pragma mark NetworkController Delegate
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"finished connection retain count: %d", [connection retainCount]);}
#pragma mark NetworkController Delegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error receiving response: %@", error);
[connection release];
}
@end
#import <Foundation/Foundation.h>
#import "NetworkController.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NetworkController *n = [[NetworkController alloc] init];
n.performConnection;
[n release];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
First, the syntax of the message:
[n performConnection];
You have syntax for accessing properties. And since the method is injected as a return one void
, I would not consider it guaranteed to work even at this level.
In any case, the main problem is that you are using the NSURLConnection asynchronous API (good), but expecting synchronous behavior (bad). All NSURLConnection methods return immediately, then run in the background, and the idea sends delegate messages to your network controller as soon as it happens, but immediately after you're done configuring, you release the network controller (in turn, the connection is leaking. since you are not release
its in your method dealloc
), then exit.
You need to start a run loop before you release the network controller to give the connection time to send the request, read the response and send some delegate messages to the network controller. You will also need cancel
and release
connect to the network controller dealloc
.
Also, you need to make this NSMutableData variable an instance variable as you need to get it from the delegate methods. Remember to also release this method dealloc
(s release
) and set it in nil
in both delegate methods that indicate the connection is complete).
a source to share