美文网首页程序员
iOS 开发-NSURLConnection代理设置注意点

iOS 开发-NSURLConnection代理设置注意点

作者: 037e3257fa3b | 来源:发表于2017-02-20 13:02 被阅读0次

1.在主线程发起网络请求,使用[NSURLConnection connectionWithRequest:request delegate:self]设置代理方法,代理方法默认是在主线程中调用的

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(@"-------");

2.在主线程中发起网络请求,使用[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]设置代理,代理方法默认是在主线程中调用的

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(@"-------");

3.发起网络请求在子线程执行,使用使用[NSURLConnection connectionWithRequest:request delegate:self]设置代理方法,代理方法默认是在子线程中调用的

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

4.在子线程中发起网络请求,使用[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]设置代理,同时还需要设置代理方法执行所在的线程[connect setDelegateQueue:[NSOperationQueue mainQueue]]

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 mainQueue]];
        
        //开始发送请求
        //如如果connect对象没有添加到runloop中,那么该方法内部会自动的添加到runloop
        //注意:如果当前的runloop没有开启,那么该方法内部会自动获得当前线程对应的runloop对象并且开启
        [connect start];
        NSLog(@"---%@----",[NSThread currentThread]);
    });

相关文章

网友评论

    本文标题:iOS 开发-NSURLConnection代理设置注意点

    本文链接:https://www.haomeiwen.com/subject/hzzswttx.html