No data returned using NSURLConnection Asynchronously

I have a hell of a time with something that seems more likely but I can't work. I am creating an iPhone that retrieves data from a web host. I am trying to establish an asynchronous connection with the host as I want to keep the device freed up during the connection. (sendSynchronousRequest freezes
phone until the request is completed.) Here is my connection code:

//temp url to see if data is returned: NSURL *theURL = [NSURL URLWithString:@"http://www.theappleblog.com/feed"]; NSURLRequest *dataRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; /* establish the connection */ theConnection = [[NSURLConnection alloc] initWithRequest:dataRequest delegate:self startImmediately:YES]; if (theConnection == nil) { NSLog(@"Connection Failure!"); self.urlData = nil; } else { self.urlData = [[NSMutableData data] retain]; } 

I have all the necessary delegation methods:

 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response { [urlData setLength:0]; UIApplication *application = [UIApplication sharedApplication]; application.networkActivityIndicatorVisible = YES; NSLog(@"Received Response!"); } 

 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)incrementalData { [self.urlData appendData:incrementalData]; NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.urlData length]]; NSLog(@"resourceData length: %d", [resourceLength intValue]); NSLog(@"filesize: %d", self.urlDataSize); NSLog(@"float filesize: %f", [self.urlDataSize floatValue]); } 

 -(void)connectionDidFinishLoading:(NSURLConnection*)connection { NSLog(@"Connection finished loading\n"); NSLog(@"Succeeded! Received %d bytes of data",[urlData length]); _isFinished = YES; UIApplication *application = [UIApplication sharedApplication]; application.networkActivityIndicatorVisible = NO; } 

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Error: %@",[error localizedDescription]); } 

As you can see, I have a boat loading log messages because I wanted to see if anything was happening. My connection check
returned as TRUE, but the data is never loaded. As I said, I’m sure that I should do (or not do) something really stupid. But what? Any help would be most appreciated.

Thanks Lawrence

+7
asynchronous objective-c iphone cocoa-touch nsurlconnection
source share
9 answers

This doesn't answer your question directly, but you might think of the Ben Copsey ASIHTTPRequest , which wraps around CFNetwork and includes asynchronous, multi-threaded request code, which requires a lot of work to get it right.

0
source share

The problem was that I continued the program flow and did not stop for the connection to complete the download. Thank you for all the suggestions.

+2
source share

A few suggestions:

  • In your first snippet, instead of theConnection try assigning self.theConnection to make sure that property accessors are called. In different places, you also change self.urlData and urlData . Perhaps they will all be harmonized by going through self .

  • You can also create the dataRequest a property and go through self to make sure it is saved.

  • self.urlData = [[NSMutableData data] retain]; - You do not need additional retention. Going through self does this for you. Additional hold will make it so that the object is stuck and leaked, even when everything was done.

  • In a related note, in your NSLogs, make sure that you print a save counter for each object. In addition, you might want to create a general cleanup procedure to free these structures (or set self.foo = nil ), so at any time if it fails, you can call this procedure to clean things up.

  • Get a copy of Charles or WireShark (or run tcpdump in the console) and view network traffic. This will help determine at what stage of the flow it is not working.

+1
source share

Is the initial thought an object that has all of these codes stored correctly? It can be a release, and thus release the connection ...

Otherwise, you should at least see the answer.

0
source share

Nothing is clearly wrong. I have a code that is almost the same and works great. The only differences that I see are the following:

  • I am using NSURLRequestUseProtocolCachePolicy instead of NSURLRequestReloadIgnoringLocalCacheData p>

  • I use connection = [[NSURLConnection alloc] initWithRequest: theRequest delegate: self]; instead of specifying startImmediately: YES (since this is the default value)

It would be helpful to know what calls to your delegate methods will be called. At least you should get at least one or the other from the connectionDidFinishLoading or connectionDidFail that is being called. If not, then something is wrong with your delegation setting - are you absolutely sure that the delegate method signatures are correct and that you are passing the correct element as a delegate?

0
source share

It is difficult from scratch to anything, since it is not clear from your question what if any of the NSLog statements is executed. Please be a little clearer about the results you get.

With a quick glance, the only thing that looks like a potential problem is that you have a race condition when setting up the connection. If the network has a particularly thin, but non-zero probability that the connection: didReceiveData: will be called before self.urlData is allocated. I would suggest allocating urlData either before the connection starts, or in -connection: didReceiveResponse: to prevent this.

If this is not fixed, you will need more information.

0
source share

You say this code works fine using the asynchronous method? Testing this url, redirecting to another, so what if you implement redirecting delegation methods? What if you are trying to use multiple urls?

0
source share

Can the application react differently? Does runloop work? What does the application do after running the request - does it return from the event handler back to runloop? (If not, you will never receive any callbacks because they are delivered via runloop.)

0
source share

NSURLConnection is designed to work with any type of protocol, not just the HTTP protocol. This means that the connection: didFail: withError is raised for connection errors, but not for protocol errors. You do not check protocol errors - you must catch them in the connection: didReceiveResponse :. The trick is that the response object that is being transmitted is actually an NSHTTPURLResponse object. If you check the status response code, I bet you are getting an HTTP error such as 404 or 500 server.

0
source share

All Articles