多线程问题 ----- UI视图 & 网络下载 同时执行操作
2014-11-27 08:36
423 查看
苹果官方文档规定:默认结果:当滑动界面时,下载图片线程被挂起,暂停操作
事件源有三种:UI界面事件源,定时器,网络下载。其中UI界面触摸事件的优先级最高
通过修改 runloop 模式,可以实现同时并发执行
显示结果:可以边滑动,边下载图片
事件源有三种: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
相关文章推荐
- 多线程问题 ----- UI视图 & NSTimer 同时执行操作
- sqlite数据库下载安装和初步操作和所遇到的问题near "sqlite3":syntax error
- 关于"试图执行系统不支持的操作"问题
- layerUI+Ajax打开页面执行操作后关闭问题
- [网络问题,截图以后再传]本实验是设计性实验。目的在于使用关系数据库标准语言SQL,掌握视图和索引的操作。
- SQLite并发操作下的分析与处理,解决database is locked,以及多线程下执行事务等问题
- 在使用win 7 无线承载网络时,启动该服务时,有时会提示:组或资源的状态不是执行请求操作的正确状态。 网上有文章指出,解决这个问题的方法是在设备管理器中启动“Microsoft托管网络虚拟适配
- Foxmail接收邮件时,报出"网络操作失败","POP3:****"问题的解决办法
- 多线程下载网络歌曲&播放歌曲&并用seekbar调节进度&显示歌曲两边的时间
- ios多线程操作(十二)—— 自定义NSOperation实现网络下载后回调
- [iOS 多线程 & 网络 - 2.4] - 大文件下载 (边下边写/暂停恢复下载/压缩解压zip/多线程下载)
- [iOS 多线程 & 网络 - 2.4] - 大文件下载 (边下边写/暂停恢复下载/压缩解压zip/多线程下载)
- QT类学习系列(6)- Qt多线程的简单实现以及不能同时处理UI的操作
- 关于多线程中涉及UI操作,导致UI线程阻塞问题
- Android(五)数据存储之五网络多线程断点下载-世事如棋,乾坤莫测,笑 ...  -  棒槌网@Android开发论坛 - Powered by
- 在Activity中开启一个线程执行网络操作出现的问题
- OC中网络传输的时候对字节流的操作 & 对字节的处理 & 野指针问题
- 多线程问题,三个线程轮流进行,每个线程进行n次操作,共执行n*n*3次操作。本代码用三军攻击做演示,通俗易懂。
- [iOS 多线程 & 网络 - 2.10] - ASI框架下载文件
- (委托事件处理)关于多线程执行显示进度条的实例(转)&&线程间操作无效: 从不是创建控件“rtxtEntryNO”的线程访问它。