iOS 同步请求和异步请求
来源:互联网 发布:linux默认用户名密码 编辑:程序博客网 时间:2024/05/22 02:25
在iOS里面,网络请求分为同步请求个异步请求,它俩的区别是:
同步请求:
等所有操作完全执行完毕才会继续执行 同步请求的弊端:会出现假死的情况,只要请求的操作没有完全执行完毕,就不会再去响应任何事件(在同一线程)
异步请求:
在程序运行的时候,会利用空闲的时间,去执行里面的操作,不会影响到同一线程里面的其它操作
同步请求代码实现:
NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"];// 实例化 请求对象 携带着 请求的数据 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // data 是服务器给我们响应返回的数据 NSURLConnection 发送请求 的类 sendSynchronousRequest 同步请求 NSData *data = [NSURLConnection <span style="color:#009900;">sendSynchronousRequest</span>:request returningResponse:nil error:nil]; UIImageView *image = [[UIImageView alloc]initWithFrame:self.view.frame]; image.contentMode = UIViewContentModeScaleAspectFit; image.image = [UIImage imageWithData:data]; [self.view addSubview:image];
异步请求代码实现:
UIImageView *image = [[UIImageView alloc]initWithFrame:self.view.frame]; image.contentMode = UIViewContentModeScaleAspectFit; [self.view addSubview:image]; NSURL *url = [NSURL URLWithString:@"http://preview.quanjing.com/is_rm001/is0997q92.jpg"]; // 实例化 请求对象 携带着 请求的数据 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 发送异步请求 需要通过 连接 异步发送 请求// NSOperationQueue 是线程 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; // sendAsynchronousRequest 发送一个异步请求 response 服务器回应的内容(回应状态的code,以及错误的信息)data 回应给客户端需要的数据 [NSURLConnection <span style="color:#009900;">sendAsynchronousRequest</span>:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSLog(@"%@",response); image.image = [UIImage imageWithData:data]; }];
0 0
- IOS 同步请求和异步请求
- IOS:同步请求和异步请求
- iOS 同步请求和异步请求
- 异步请求和同步请求
- 同步请求和异步请求
- 同步请求和异步请求
- 同步请求和异步请求
- 同步和异步请求
- IOS同步请求和异步请求的区别
- iOS同步请求和异步请求的区别
- IOS同步请求和异步请求的区别
- ios中的ASIHTTPRequest的同步请求和异步请求
- IOS同步请求和异步请求的区别
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- IOS之同步请求、异步请求、GET请求、POST请求
- C++ Primer Plus 课后习题 第三章
- MAP学习--POJ1002
- Dom 经典实例
- Matlab求方差,均值,均方差,协方差的函数
- 我和刘强东的故事2:这次是真的
- iOS 同步请求和异步请求
- 设计模式学习笔记--23种设计模式(一)
- 数据结构的基本类型
- 给cin/cout提速
- hdu 5479(括号问题)
- 我的linux系统配置
- C++命名空间
- 数据模型设计
- PAT1006 换个格式输出整数