异步 NSURLConnection 方案教程 [英] Asynchronous NSURLConnection Scheme Tutorial

查看:50
本文介绍了异步 NSURLConnection 方案教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找关于使用 NSURLConnection 异步请求的好教程.我一直在 stackoverflow 和 Google 上四处寻找,但没有找到.这可能是这里无数类似问题的重复.但是请指导我正确的教程,我之前使用过 ASIHTTPRequest,但我之前没有使用过 Apple 提供的库.

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.

推荐答案

我会给你提供一个自己写的,但我强烈推荐使用 AFNetworking,它是 NSURLConnection/NSURLRequest 系统之上的一个包装器,具有更清晰的流程,您还可以使用基本的 NSURLRequests/Connections 以及常规的 NSOperationQueues.图书馆也使用可可豆荚,说实话,你真的不能再干净了.

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]);
    }
}];

mainQueue 对象用于路由请求和管理一次可以发送多少.这可以以多种方式使用,我倾向于将它们用于分类请求(身份验证、主要、上传队列)

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)

一旦进入该块,您就可以使用返回的响应构建本地 NSHTTPURLResponse.如果您希望返回状态代码,则需要这样做.(在标准 NSURLResponse 对象中不存在)

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 是通常可以直接转换为字符串或通过反序列化器运行以获得人类可读数据的数据.

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) :-)

就像总是委托或阻止一样,您希望在收到响应后触发 UI 更新,但不要等待请求完成,如果您正在将数据加载到表中,您将在加载时调用请求并提供某种形式的进度 hud 告诉他们正在发出请求,一旦收到数据,您就删除 hud 并重新加载表数据.HUD 必须在主线程上调用,因此您肯定需要处理它,我通常只构建扩展并使用 performSelectorOnMainThread,但是更好的方法可能是将您的函数包装在 dispatch_async 中并调用您在 hud 之外显示/隐藏代码

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.

这篇关于异步 NSURLConnection 方案教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆