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!
ios iphone nsurlconnection
C.Johns Feb 14 '12 at 1:17 2012-02-14 01:17
source share