·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> iOS中使用NSURLConnection处理HTTP同步与异步请求

iOS中使用NSURLConnection处理HTTP同步与异步请求

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

一、引言

在iOS7后,NSURLSession基本代替了NSURLConnection进行网络开发,在iOS9后,NSURLConnection相关方法被完全的弃用,iOS系统有向下兼容的特性,尽管NSURLConnection已经被弃用,但在开发中,其方法依然可以被使用,并且如果需要兼容到很低版本的iOS系统,有时就必须使用NSURLConnection类了。

二、使用NSURLConnection进行同步请求

对于网络请求分为同步和异步两种,同步是指在请求结果返回之前,程序代码会卡在请求处,之后的代码不会被执行,异步是指在发送请求之后,一边在子线程中接收返回数据,一边执行之后的代码,当返回数据接收完毕后,采用回调的方式通知主线程做处理。

使用如下方法进行NSURLConnection的同步请求:

    NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSLog(@"%@",data);
    NSLog(@"继续执行");
打印信息如下图所示,从中可以看出,当数据返回结束时才执行后面的代码:

20167491116117.png (425×108)

三、使用NSURLConnection进行异步请求

使用同步的方式进行请求有一个很大的弊端,在进行网络请求时,数据的返回往往需要一定时间,不可能瞬间完成,使用同步的方式将导致界面卡死,没有提示也不能交互任何用户操作,这样的话,很有可能会给用户程序卡死的假象。

NSURLConnection类提供两种方式进行异步请求操作。

1.使用block的方式进行异步请求

使用如下代码进行block方式的异步请求,在block中会传入请求到的返回数据和数据信息等参数:

    NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    //其中的queue参数决定block中的代码在哪个队列中执行
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSLog(@"%@",data);
    }];
    NSLog(@"继续执行");

2.使用代理回调的异步请求方式

首先遵守协议与生命一个可变的NSData用于接收数据:

@interface ViewController ()<NSURLConnectionDataDelegate>
{
    NSMutableData * _data;
}
@end
使用如下的代码进行请求:

    _data = [[NSMutableData alloc]init];
    NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];
请求发出后,会一次调用如下代理方法进行请求过程的监听和数据的获取:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    //开始接收数据
    [_data setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    //正在接收数据
    [_data appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    //接收数据失败
    NSLog(@"%@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    //接收数据完成
    NSLog(@"%@",_data);
}

四、示例
1.通过NSURLConnection进行异步下载:  
NSURLConnection 提供了两种方式来实现连接,一种是同步的另一种是异步的,异步的连接将会创建一个新的线程,这个线程将会来负责下载的动作。而对于同步连接,在下载连接和处理通讯时,则会阻塞当前调用线程。
许多开发者都会认为同步的连接将会堵塞主线程,其实这种观点是错误的。一个同步的连接是会阻塞调用它的线程。如果你在主线程中创建一个同步连接,没错,主线程会阻塞。但是如果你并不是从主线程开启的一个同步的连接,它将会类似异步的连接一样。因此这种情况并不会堵塞你的主线程。事实上,同步和异步的主要区别就是运行 runtime 为会异步连接创建一个线程,而同步连接则不会。

//asynchronousRequest connection 
-(void)fetchAppleHtml{ 
    NSString *urlString = @"http://www.apple.com"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f]; //maximal timeout is 30s 
     
    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
        if ([data length] > 0 && connectionError == nil) { 
            NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
            NSString *filePath = [documentsDir stringByAppendingPathComponent:@"apple.html"]; 
            [data writeToFile:filePath atomically:YES]; 
            NSLog(@"Successfully saved the file to %@",filePath); 
            NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
            NSLog(@"HTML = %@",html); 
        }else if ([data length] == 0 && connectionError == nil){ 
            NSLog(@"Nothing was downloaded."); 
        }else if (connectionError != nil){ 
            NSLog(@"Error happened = %@",connectionError); 
        } 
    }]; 

2.通过NSURLConnection进行同步下载:
使用 NSURLConnection 的 sendSynchronousRequest:returningResponse:error:类方法,我们可以进行同步请求。在创建一个同步的网络连接的时候我们需要明白一点,并不是是我们的这个同步连接一定会堵塞我们的主线程,如果这个同步的连接是创建在主线程上的,那么这种情况下是会堵塞我们的主线程的,其他的情况下是不一定会堵塞我们的主线程的。如果你在 GCD 的全局并发队列上初始化了一个同步的连接,你其实并不会堵塞我们的主线程的。
我们来初始化第一个同步连接,并看看会发生什么。在实例中,我们将尝试获取 Yahoo!美国站点主页内容:

//synchronousRequest connection 
-(void)fetchYahooData{ 
    NSLog(@"We are here..."); 
    NSString *urlString = @"http://www.yahoo.com"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
    NSURLResponse *response = nil; 
    NSError *error = nil; 
    NSLog(@"Firing synchronous url connection..."); 
    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
    if ([data length] > 0 && error == nil) { 
        NSLog(@"%lu bytes of data was returned.",(unsigned long)[data length]); 
    }else if([data length] == 0 && error == nil){ 
        NSLog(@"No data was return."); 
    }else if (error != nil){ 
        NSLog(@"Error happened = %@",error); 
    } 
    NSLog(@"We are done."); 
     

/*
 |
 | as we know, it will chock main thread when we call sendSynchronousRequest on main thread,,,,change below
 |
 v
*/ 
//call sendSynchronousRequest on GCD pool 
-(void)fetchYahooData2_GCD{ 
    NSLog(@"We are here..."); 
    NSString *urlString = @"http://www.yahoo.com"; 
    NSLog(@"Firing synchronous url connection..."); 
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(dispatchQueue, ^{ 
        NSURL *url = [NSURL URLWithString:urlString]; 
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
        NSURLResponse *response = nil; 
        NSError *error = nil; 
        NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; 
        if ([data length] > 0 && error == nil) { 
            NSLog(@"%lu bytes of data was returned.",(unsigned long)[data length]); 
        }else if ([data length] == 0 && error == nil){ 
            NSLog(@"No data was returned."); 
        }else if (error != nil){ 
            NSLog(@"Error happened = %@",error); 
        } 
    }); 
    NSLog(@"We are done."); 
 

查看运行输出结果,分别为:
synchronous download on main thread without GCD

20167491155165.jpg (573×58)

synchronous download on main thread with GCD

20167491216196.jpg (571×65)

可以看到在主线程上调用同步下载会阻塞当前线程,而使用GCD则不会。