多线程问题 ----- UI视图 & 网络下载 同时执行操作


苹果官方文档规定:默认结果:当滑动界面时,下载图片线程被挂起,暂停操作

事件源有三种:UI界面事件源,定时器,网络下载。其中UI界面触摸事件的优先级最高



通过修改 runloop 模式,可以实现同时并发执行

显示结果:可以边滑动,边下载图片





//
//  RootViewController.m
//  RunLoopConnectionDemo
//
//  Created by mac on 14-11-26.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<NSURLConnectionDataDelegate>
{
    BOOL _downloadFinished;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    /**
     * 让ScrollView的滑动跟下载共存
     */
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [self.view addSubview:scrollView];
    for (int i = 0; i < 3; i++) {
        float x = i * 320;
        float y = 0;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 320, 40)];
        float radomColor = (arc4random() % 255) / 255.0;
        UIColor *color = [UIColor colorWithRed:radomColor green:radomColor blue:radomColor alpha:1.0];
        view.backgroundColor = color;
        [scrollView addSubview:view];
    }
    scrollView.contentSize = CGSizeMake(320 * 3, 0);
    
    //网络下载
    NSString *strURL = @"http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V2.4.1.dmg";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
    
    /**
     * 默认就在主线程的DefaultRunLoopModel里面
     * Runloop Model 不能修改
     */
//    [NSURLConnection connectionWithRequest:request delegate:self];
    
    /**
     * 如果想ScrollView滑动的时候也能接收到服务器数据
     * 必须要以下面这种创建NSURLConnection对象
     */
//    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    
    //更改NSURLConnection运行的RunLoop Model
//    [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
//    [connection start];
    
    //子线程下载
    [self performSelectorInBackground:@selector(downloadInThread) withObject:nil];
}

#pragma mark - 子线程里面下载数据
- (void)downloadInThread
{
    NSString *strURL = @"http://dl_dir.qq.com/qqfile/qq/QQforMac/QQ_V2.4.1.dmg";
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    
    [connection start];
    
    NSLog(@"子线程下载开始");
    
    while (!_downloadFinished) {
        /**
         * 修改子线程的RunLoop模式,让线程有事做事,没事就挂起
         * RunLoop接收到事件,会通知线程,让线程运行
         * 如果没有事件线程就挂起
         */
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    NSLog(@"子线程死掉了");
}

#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"建立起跟服务器的连接");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    static int count = 0;
    count++;
    NSLog(@"收到数据:%d", count);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"数据接收完毕");
    
    _downloadFinished = YES;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"出现错误%@", error);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值