How to use sendAsynchronousRequest: queue: completeHandler:

Two-Part Question

Part One: I'm trying to create an ASynchronous query on my database. I am currently doing this synchronously, but I want to learn both ways in order to better understand what is happening.

Currently, I configured my synchronous call as follows.

- (IBAction)setRequestString:(NSString *)string { //Set database address NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8778/instacodeData/"]; // imac development //PHP file name is being set from the parent view [databaseURL appendString:string]; //call ASIHTTP delegates (Used to connect to database) NSURL *url = [NSURL URLWithString:databaseURL]; //SynchronousRequest to grab the data NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSError *error; NSURLResponse *response; NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (!result) { //Display error message here NSLog(@"Error"); } else { //TODO: set up stuff that needs to work on the data here. NSString* newStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; NSLog(@"%@", newStr); } } 

I think what I need to do is replace the call

 NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

with ASynchronous version

 sendAsynchronousRequest:queue:completionHandler: 

however, I am not sure what to queue or terminate Handler ... Any examples / solutions would be greatly appreciated.

Part Two: I read about several tasks, and I would like to support it, making sure my connection requests are complete if there is an interrupt. I followed this

It explains how to get more time if an interruption occurs, I understand what it does .. but not how to apply it to this connection? if you have examples / tutorials to help me figure out how to apply it, that would be great!

+73
ios iphone nsurlconnection
Feb 14 '12 at 1:17
source share
6 answers

PArt 1:

 NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] > 0 && error == nil) [delegate receivedData:data]; else if ([data length] == 0 && error == nil) [delegate emptyReply]; else if (error != nil && error.code == ERROR_CODE_TIMEOUT) [delegate timedOut]; else if (error != nil) [delegate downloadError:error]; }]; 
+112
Feb 14 2018-12-12T00:
source share

Here is an example:

 NSString *urlAsString = @"http://www.cnn.com"; NSURL *url = [NSURL URLWithString:urlAsString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] >0 && error == nil) { // DO YOUR WORK HERE } else if ([data length] == 0 && error == nil) { NSLog(@"Nothing was downloaded."); } else if (error != nil){ NSLog(@"Error = %@", error); } }]; 
+37
May 2, '12 at 15:45
source share

For the queue parameter, try this magic:

 [NSOperationQueue mainQueue] 

This works great if you update the user interface after completing the request, since the main queue is the main thread. This essentially gives you the previous behavior of NSURLConnection. If, however, you plan to write the file or decompress it, then you can complete it in the background and then send the asynchronous request back to the main queue for user interface updates.

+25
Jun 25
source share

I am working on a similar problem, I posted this question and got a clear answer here , I hope this helps with part 2.

For part 1, the others mentioned here are good, but you need to add one more check (I changed the answer below). Perhaps your request will return, say, error 404 (page not found), and in this case you will not receive, and the error and data will be> 0. 200 is a good answer, you can also check StatusCode for 404 or whatever.

  [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if ([data length] >0 && error == nil && [httpResponse statusCode] == 200) { // DO YOUR WORK HERE } 
+9
May 28 '13 at 15:24
source share

Since sendAsynchronousRequest:urlRequest queue:queue completionHandler: deprecated in iOS 9, and he will suggest using NSURLSession -dataTaskWithRequest:completionHandler: instead. It is available with iOS 7 and later .

Original:

  NSURL *URL = [NSURL URLWithString:@"http://example.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // ... }]; 

Using NSURLSession:

  NSURL *URL = [NSURL URLWithString:@"http://example.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { // ... }]; [task resume]; 
+3
Apr 20 '16 at 6:16
source share

sendAsynchronousRequest deprecated in Swift. Navigate to dataTaskWithRequest , fortunately, it is used in much the same way.

 if let url = NSURL(string:"http://your_url") { let request:NSURLRequest = NSURLRequest(URL: url) let config = NSURLSessionConfiguration.defaultSessionConfiguration() let session = NSURLSession(configuration: config) let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in }); task.resume() } 
+2
Oct 18 '16 at 4:49 on
source share



All Articles