ios开发网络学习六:设置队列请求与RunLoop
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> @end @implementation ViewController -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self newThreadDelegate2];
} -(void)delegate1
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //设置代理
//代理方法:默认是在主线程中调用的
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self]; //设置代理方法在哪个线程中调用
//[NSOperationQueue alloc]init]] 开子线程
//[NSOperationQueue mainQueue] 不能这样设置
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
//[connect setDelegateQueue:[NSOperationQueue mainQueue]]; NSLog(@"-------");
} -(void)delegate2
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //设置代理
//代理方法:默认是在主线程中调用的
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]; [connect setDelegateQueue:[[NSOperationQueue alloc]init]]; //开始发送请求
[connect start];
NSLog(@"-------");
} -(void)newThreadDelegate1
{
dispatch_async(dispatch_get_global_queue(, ), ^{ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //设置代理
//代理方法:默认是在主线程中调用的
//该方法内部其实会将connect对象作为一个source添加到当前的runloop中,指定运行模式为默认
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self]; //设置代理方法在哪个线程中调用
[connect setDelegateQueue:[[NSOperationQueue alloc]init]]; //[[NSRunLoop currentRunLoop] runMode:UITrackingRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1000]];
[[NSRunLoop currentRunLoop]run]; NSLog(@"---%@----",[NSThread currentThread]);
}); } -(void)newThreadDelegate2
{
dispatch_async(dispatch_get_global_queue(, ), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]]; //设置代理
//代理方法:默认是在主线程中调用的
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]; [connect setDelegateQueue:[[NSOperationQueue alloc]init]]; //开始发送请求
//如如果connect对象没有添加到runloop中,那么该方法内部会自动的添加到runloop
//注意:如果当前的runloop没有开启,那么该方法内部会自动获得当前线程对应的runloop对象并且开启
[connect start];
NSLog(@"---%@----",[NSThread currentThread]);
});
} #pragma mark ----------------------
#pragma mark NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse---%@",[NSThread currentThread]);
} @end
#####1 NSURLConnection和Runloop(面试)
(1)两种为NSURLConnection设置代理方式的区别
```objc
//第一种设置方式:
//通过该方法设置代理,会自动的发送请求
// [[NSURLConnection alloc]initWithRequest:request delegate:self];
//第二种设置方式:
//设置代理,startImmediately为NO的时候,该方法不会自动发送请求
NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO];
//手动通过代码的方式来发送请求
//注意该方法内部会自动的把connect添加到当前线程的RunLoop中在默认模式下执行
[connect start];
```
(2)如何控制代理方法在哪个线程调用
```objc
//说明:默认情况下,代理方法会在主线程中进行调用(为了方便开发者拿到数据后处理一些刷新UI的操作不需要考虑到线程间通信)
//设置代理方法的执行队列
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
```
(3)开子线程发送网络请求的注意点,适用于自动发送网络请求模式
```objc
//在子线程中发送网络请求-调用startf方法发送
-(void)createNewThreadSendConnect1
{
//1.创建一个非主队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作,并把任务添加到队列中执行
[queue addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
//2-1.确定请求路径
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
//2-2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2-3.使用NSURLConnection设置代理,发送网络请求
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
//2-4.设置代理方法在哪个队列中执行,如果是非主队列,那么代理方法将再子线程中执行
[connection setDelegateQueue:[[NSOperationQueue alloc]init]];
//2-5.发送网络请求
//注意:start方法内部会把当前的connect对象作为一个source添加到当前线程对应的runloop中
//区别在于,如果调用start方法开发送网络请求,那么再添加source的过程中,如果当前runloop不存在
//那么该方法内部会自动创建一个当前线程对应的runloop,并启动。
[connection start];
}];
}
//在子线程中发送网络请求-自动发送网络请求
-(void)createNewThreadSendConnect2
{
NSLog(@"-----");
//1.创建一个非主队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//2.封装操作,并把任务添加到队列中执行
[queue addOperationWithBlock:^{
//2-1.确定请求路径
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=dd&pwd=ww&type=JSON"];
//2-2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//2-3.使用NSURLConnection设置代理,发送网络请求
//注意:该方法内部虽然会把connection添加到runloop,但是如果当前的runloop不存在,那么不会主动创建。
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
//2-4.设置代理方法在哪个队列中执行,如果是非主队列,那么代理方法将再子线程中执行
[connection setDelegateQueue:[[NSOperationQueue alloc]init]];
//2-5 创建当前线程对应的runloop,并开启
[[NSRunLoop currentRunLoop]run];
}];
}
```
总结:也就是说发送网络请求在设置回调的队列的时候,回调的任务默认在主线程,若是设置回调队列在子线程,则毁掉成功后不会调用代理方法,解决办法:1:调用start方法: start方法内部会把当前的connect对象作为一个source添加到当前线程对应的runloop中,
如果调用start方法开发送网络请求,那么再添加source的过程中,如果当前runloop不存在那么该方法内部会自动创建一个当前线程对应的runloop,并启动。每条线程都必须有自己的runloop来处理事件 2:若是不调用start方法,则需要自己创建runloop并调用run方法,
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON"]];
//设置代理
//代理方法:默认是在主线程中调用的
//该方法内部其实会将connect对象作为一个source添加到当前的runloop中,指定运行模式为默认
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self];
//设置代理方法在哪个线程中调用
[connect setDelegateQueue:[[NSOperationQueue alloc]init]];
//[[NSRunLoop currentRunLoop] runMode:UITrackingRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1000]];
[[NSRunLoop currentRunLoop]run];
NSLog(@"---%@----",[NSThread currentThread]);
});
}
二者的区别就在于:1:调用start如果调用start方法开发送网络请求,那么再添加source的过程中,如果当前runloop不存在那么该方法内部会自动创建一个当前线程对应的runloop,并启动 2:而下面的方法虽自动发送请求,也会
将connect对象作为一个source添加到当前的runloop中,指定运行模式为默认,但是如果当前子线程的runloop不存在则不会自动创建需要手动创建
NSURLConnection *connect = [NSURLConnection connectionWithRequest:request delegate:self];
ios开发网络学习六:设置队列请求与RunLoop的更多相关文章
- ios开发网络学习:一:NSURLConnection发送GET,POST请求
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- iOS开发——网络篇——HTTP/NSURLConnection(请求、响应)、http响应状态码大全
一.网络基础 1.基本概念> 为什么要学习网络编程在移动互联网时代,移动应用的特征有几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图只有通过网络跟外界进行数据交互.数据更新, ...
- ios开发网络学习十二:NSURLSession实现文件上传
#import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @&q ...
- ios开发网络学习五:输出流以及文件上传
一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...
- ios开发网络学习四:NSURLConnection大文件断点下载
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- ios开发网络学习AFN框架的使用一:get和post请求
#import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...
- iOS开发网络学习七:NSURLSession的基本使用get和post请求
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...
- ios开发网络学习十一:NSURLSessionDataTask离线断点下载(断点续传)
#import "ViewController.h" #define FileName @"121212.mp4" @interface ViewControl ...
- ios开发网络学习九:NSURLSessionDownloadTask实现大文件下载
一:NSURLSessionDownloadTask:实现文件下载:无法监听进度 #import "ViewController.h" @interface ViewControl ...
随机推荐
- 联合体union用在何处?
程序设计刚開始学习的人在学习时,总想问:"这个东东有什么用?"于是,在建设有关的教学资源时,也便总从这个角度,试图给出一些案例,这是一个将刚開始学习的人作为教学目标人群的人该干的事 ...
- IE兼容性开发的笔记
当前项目组开发的产品对外承诺支持IE9和IE11,但在推广应用过程中发现存在相当比例的用户实际上还在使用IE8.而这相当比例中的用户还包含了大部分的公司领导.为了满足公司内部各阶层人士体验我们产品的诉 ...
- 分享一些 WINDOWS 资源站点(备用)
分享一些 WINDOWS 资源站点(备用) Windows Embedded http://www.10down.net/windows-embedded.php Windows 7 SP1 Upd ...
- checkbox-padding 调整checkbox字体跟图标距离
有时候我们会遇到需要调整控件中的内容相对于容器的位置.这里有两种情况 1.linearlayout这样的容器中,包含button类的控件,这时候margin可以调节 2.textview中的文字内容 ...
- ACM 中JAVA的应用
原文地址:http://www.cppblog.com/vontroy/archive/2010/05/24/116233.html 先说一下Java对于ACM的一些优点吧: (1) 对于熟悉C/C+ ...
- 字串乱序 PHP&JS
<?php /** * 字串乱序 PHP&JS * * php 中把字串乱序后输出给客户机的 JAVASCRIPT , JAVASCRIPT 中恢复 * 在指定长度提取一个字符,并把这一 ...
- python-string中部分string替换
今天遇到一个问题,就是需要把 “/home/zhangshuli/32_kk/” 中的32_kk 替换成为 52_kk 然后就在网上找方法,刚开始尝试的方法是 aaa = "/home/zh ...
- 洛谷 P1601 A+B Problem(高精)
P1601 A+B Problem(高精) 题目背景 无 题目描述 高精度加法,x相当于a+b problem,[b][color=red]不用考虑负数[/color][/b] 输入输出格式 输入格式 ...
- 很吊炸天的Xcode插件,你想要的这都有
整理自BigPolarBear的博客 .杂技杂记 以及CC此前整理. 古人云"工欲善其事必先利其器",打造一个强大的开发环境.是马上提升自身战斗力的绝佳途径!下面是搜集的一些有力 ...
- Android 获取联系人手机号码、姓名、地址、公司、邮箱、生日
public void testGetAllContact() throws Throwable { //获取联系人信息的Uri Uri uri = ContactsContract.Contacts ...