当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS中使用NSURLConnection处理HTTP同步与异步请求

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

深秋钓鲫鱼,狼狈伴郎,藏龙出海

一、引言

在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则不会。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网