Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone: Downloading data asynchronously with NSURL request as opposed to [NSData datawithContents ofURL]
    text
    copied!<p>Hi Im a newbie and Im trying to download geocoding data from googleapi webpage. I did it using this: code:</p> <pre><code> NSMutableString *urlStr = [[NSMutableString alloc] initWithString:temp]; NSMutableString *address = [[NSMutableString alloc] initWithString:l.Address]; [address appendString:@" "]; [address appendString:l.CityName]; [address appendString:@" "]; [address appendString:l.State]; [address appendString:@" "]; [address appendString:l.PostalCode]; NSArray *addressArray = [address componentsSeparatedByString:@" "]; [address release]; NSString *a = [addressArray componentsJoinedByString:@"+"]; [urlStr appendString:a]; [urlStr appendString:@"&amp;sensor=false"]; NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSData *data = [NSData dataWithContentsOfURL:url]; </code></pre> <p>It worked fine but the glitch was that it locked up my GUI since it was all done synchronously.</p> <p>Someone suggested I do it asynchronously using NSURLrequest. So I went to the apple website. from there I saw their example. However, Im still stuck. How do I convert the above code which is a synchronous request to an asynchronous one? So far this is what I have, but Im not sure if this works...... a bit confused ... could someone please point me in the righ direction with this code?</p> <pre><code> NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; //create NSURL request for asynchronous processing NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if (theConnection) { receivedData = [[NSMutableData data] retain]; } else { // Inform the user that the connection failed. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload