ios开发网络学习六:设置队列请求与RunLoop

摘要:
username=123&pwd=123&type=JSON"]];//设置代理//代理方法:默认是在主线程中调用的NSURLConnection*connect=[[NSURLConnectionalloc]initWithRequest:requestdelegate:selfstartImmediately:NO];[connectsetDelegateQueue:[[NSOperationQueuealloc]init]];//开始发送请求[connectstart];NSLog;}-newThreadDelegate1{dispatch_async(dispatch_get_global_queue(0,0),^{NSURLRequest*request=[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://120.25.226.186:32812/login?
#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>

@end

@implementationViewController

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

-(void)newThreadDelegate2
{
    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"]];
        
        //设置代理
        //代理方法:默认是在主线程中调用的
        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》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C语言实现进度条linux后台执行命令:&amp;amp;和nohup下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java内存模型(JMM)详解

在Java JVM系列文章中有朋友问为什么要JVM,Java虚拟机不是已经帮我们处理好了么?同样,学习Java内存模型也有同样的问题,为什么要学习Java内存模型。它们的答案是一致的:能够让我们更好的理解底层原理,写出更高效的代码。 就Java内存模型而言,它是深入了解Java并发编程的先决条件。对于后续多线程中的线程安全、同步异步处理等更是大有裨益。 硬...

使用 Python 和 Oracle 数据库实现高并发性

随着趋势发展的核心转向更多而不是更快发展,最大限度地提高并发性的重要性日益凸显。并发性使得编程模式发生了新的转变,可以编写异步代码,从而将多个任务分散到一组线程或进程中并行工作。如果您不是编程新手并且很熟悉 C 或 C++,您可能已经对线程和进程有所了解,并且知道它们之间的区别。在进行并发编程时,线程提供了进程的轻量级替代物,在大多数情况下多线程较多进程更...

epoll惊群原因分析

考虑如下情况(实际一般不会做,这里只是举个例子): 在主线程中创建一个socket、绑定到本地端口并监听 在主线程中创建一个epoll实例(epoll_create(2)) 将监听socket添加到epoll中(epoll_ctl(2)) 创建多个子线程,每个子线程都共享步骤2里创建的同一个epoll文件描述符,然后调用epoll_wait(2)等待事...

python——多线程与线程锁

一、概念: 进程:对系统过来说,一个任务是一个进程; 线程:线程是包含在进程中。进程中,线程来工作,线程是最小的单位。一个进程可以启多个线程 二、进程和线程使用时的选择: 1.cpu密集型任务,用进程 #比如排序、计算的功能2.IO密集型任务,多线程 input Output #读写频繁,网络IO或者磁盘IO。浪费CPU比较少的用多线程3.如果需要读写也需...

黑马程序员——JAVA学习笔记六(多线程)

1,    什么是多线程?一个程序可以执行多个任务,每一个任务称为一个线程,运行多个线程的程序称为多线程程序。 进程:正在进行中的程序(直译)。 线程:进程中一个负责程序执行的控制单元(执行路径)。   多线程的好处:解决了多部分代码同时运行的问题。多线程的弊端:线程太多,会导致效率的降低。 其实,多个应用程序同时执行都是CPU在做着快速的切换完成的。这个...

iOS开发 贝塞尔曲线UIBezierPath(后记)

使用CAShapeLayer与UIBezierPath可以实现不在view的drawRect方法中就画出一些想要的图形 。 1:UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个OC封装。使用此类可以定义常见的圆形、多边形等...