Asynchronous NSURLConnection Scheme Tutorial

廉价感情. 提交于 2019-12-10 18:08:01

问题


I am looking for a good tutorial on using the NSURLConnection Asynchronous request. I have been looking around in stackoverflow and Google but could not find one. This can be a duplicate of zillions of questions like this here. But please direct me to the correct tutorial, I have used ASIHTTPRequest before, but I have not used the Apple provided library before.


回答1:


I would provide you with one written myself, however I would HIGHLY recommend using AFNetworking, it's a wrapper above the NSURLConnection / NSURLRequest system that has a much cleaner flow, you can also use basic NSURLRequests / Connections with this, along with regular NSOperationQueues. The library also uses cocoa pods, and to be honest you really can't get much cleaner then 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 for routing requests and managing how many can be sent at once. This can be used in many ways, I tend to use them for categorized request (Authentication, Main, Upload Queue)

once inside the block you build a local NSHTTPURLResponse using the returned response. This is needed if you want the status code returned. (doesn't exist in the standard NSURLResponse object)

responseData is the data that can usually be converted right to a string or run through a deserializer to obtain human readable data.

Pretty simple explanation, delegates get you in trouble if you have no idea how to manage multiple requests from the same object (probably why I prefer blocks) :-)

Like always delegates or blocks you want to trigger your UI to update after you receive the response, but not be held back waiting for the request to complete, if you were loading data into a table you would call the request on load and supply some form of progress hud telling them a request is being made, once the data is received you remove the hud and reload the table data. HUDs MUST be called on the main thread so you will definitely need to handle that, I usually just build extensions and use performSelectorOnMainThread, however a better way might be to wrap your function in dispatch_async and call you're hud show / hide code outside of that.



来源:https://stackoverflow.com/questions/14902102/asynchronous-nsurlconnection-scheme-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!