您的位置:首页 > 理论基础 > 计算机网络

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

2014-11-27 08:36 423 查看
苹果官方文档规定:默认结果:当滑动界面时,下载图片线程被挂起,暂停操作

事件源有三种: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐