Asynchronous NSURLConnection Curriculum

I'm looking for a good tutorial on using the NSURLConnection asynchronous request. I've looked around on stackoverflow and google but couldn't find it. This may be a duplicate of questions like the one here. But please direct me to the correct tutorial, I have used ASIHTTPRequest before, but I have not used the previously provided Apple library before.

+3


source to share


1 answer


I would provide you with one written by me, however I would highly recommend using AFNetworking , it is a wrapper over the NSURLConnection / NSURLRequest system which has a much cleaner flow, you can also use basic NSURLRequests / Connections along with regular NSOperationQueues. The library uses cocoa pods as well and to be honest you really can't get much cleaner than that.

NSOperationQueue *mainQueue = [[NSOperationQueue alloc] init];
[mainQueue setMaxConcurrentOperationCount:5];

NSURL *url = [NSURL URLWithString:@"http://192.168.0.63:7070/api/Misc/GetFuelTypes"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

[request setHTTPMethod:@"GET"];
[request setAllHTTPHeaderFields:@{@"Accepts-Encoding": @"gzip", @"Accept": @"application/json"}];

[NSURLConnection sendAsynchronousRequest:request queue:mainQueue completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) {
    NSHTTPURLResponse *urlResponse = (NSHTTPURLResponse *)response;
    if (!error) {
        NSLog(@"Status Code: %li %@", (long)urlResponse.statusCode, [NSHTTPURLResponse localizedStringForStatusCode:urlResponse.statusCode]);
        NSLog(@"Response Body: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
    }
    else {
        NSLog(@"An error occured, Status Code: %i", urlResponse.statusCode);
        NSLog(@"Description: %@", [error localizedDescription]);
        NSLog(@"Response Body: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
    }
}];

      

The mainQueue object is used to route requests and manage the number of times sent at the same time. This can be used in a variety of ways, I tend to use them for a categorized request (authentication, main, upload queue)

Once inside the block, you create a local NSHTTPURLResponse request using the returned response. This is required if you want to return a status code. (does not exist in standard NSURLResponse object)



responseData is data that can usually be converted directly to a string or executed through a deserializer to obtain human readable data.

Quite a simple explanation, delegates are causing you problems if you don't know how to handle multiple requests for the same object (perhaps why I prefer blocks) :-)

As always the delegates or blocks you want to activate so that your UI is refreshed upon receiving a response, but don't hold back waiting for the request to complete, if you were loading data into a table, you would invoke a load request and provide some form of progress telling them that the query is executed, after getting the data you delete the hud and reload the table data. HUDs MUST be called on the main thread so you will definitely need to handle this, I usually just create extensions and use performSelectorOnMainThread, however the best way might be to wrap your function in dispatch_async and call you is to show / hide the code outside of that.

+6


source







All Articles