iOS8 多线程Multithreading简介
2015-01-12 01:21
351 查看
多线程(Multithreading)
队列(queues)
在iOS中多线程主要是关于“队列”。队列中使用“块”。块被队列提取出来,在相关的线程上执行。
主队列(main queue)
所有的UI活动都必须且只能在在主队列上执行。
相对地,那些消耗内存的非UI活动必须在其他队列上执行。
总之,原则是让UI响应起来。
其他的队列
多数情况下,iOS帮我们建立好了其他的队列。
1.两种方式建立主队列
dispatch_queue_t mainQ = dispatch_get_main_queue();
NSOperationQueue *mainQ = [NSOperationQueue mainQueue];
2.建立其他队列
dispatch_queue_t otherQ = dispatch_queue_create(“name”, NULL);//name是字符型常量
3.在主队列上调用方法
- (void)performSelectorOnMainThread:(SEL)aMethod
withObject:(id)obj
waitUntilDone:(BOOL)waitUntilDone; dispatch_async(dispatch_get_main_queue(), ^{ /*调用的方法 */ });
4.多线程示例
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@“http://...”]];
NSURLConfiguration *configuration = …;
//下面在主队列上建立session,这样就可以在task中处理UI
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
/* 可以在此处处理UI/ }];
//如果要在此处处理一些非UI进程,最好很快, 千万不要阻碍主队列!
[task resume];
当然,如果session不在主队列,而你想做一些UI的事情,那么可以在主队列上使用“块”。
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; //不在主队列上
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{ /* do UI things */ });
or [self performSelectorOnMainThread:@selector(doUIthings) withObject:nil waitUntilDone:NO];
}];
[task resume];
队列(queues)
在iOS中多线程主要是关于“队列”。队列中使用“块”。块被队列提取出来,在相关的线程上执行。
主队列(main queue)
所有的UI活动都必须且只能在在主队列上执行。
相对地,那些消耗内存的非UI活动必须在其他队列上执行。
总之,原则是让UI响应起来。
其他的队列
多数情况下,iOS帮我们建立好了其他的队列。
1.两种方式建立主队列
dispatch_queue_t mainQ = dispatch_get_main_queue();
NSOperationQueue *mainQ = [NSOperationQueue mainQueue];
2.建立其他队列
dispatch_queue_t otherQ = dispatch_queue_create(“name”, NULL);//name是字符型常量
3.在主队列上调用方法
- (void)performSelectorOnMainThread:(SEL)aMethod
withObject:(id)obj
waitUntilDone:(BOOL)waitUntilDone; dispatch_async(dispatch_get_main_queue(), ^{ /*调用的方法 */ });
4.多线程示例
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL urlWithString:@“http://...”]];
NSURLConfiguration *configuration = …;
//下面在主队列上建立session,这样就可以在task中处理UI
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
/* 可以在此处处理UI/ }];
//如果要在此处处理一些非UI进程,最好很快, 千万不要阻碍主队列!
[task resume];
当然,如果session不在主队列,而你想做一些UI的事情,那么可以在主队列上使用“块”。
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; //不在主队列上
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{ /* do UI things */ });
or [self performSelectorOnMainThread:@selector(doUIthings) withObject:nil waitUntilDone:NO];
}];
[task resume];
相关文章推荐
- iOS8 NotificationCenter Extension 简介
- MFC程序中消息以及函数的处理顺序简介!
- 数据结构之:treap 简介(二)
- 【下载中心】如何撰写分卷资源的标题、简介 推荐
- TCP/IP 简介
- 简介
- WinLogon登录管理和GINA简介
- cmmi简介以及5个级别
- kvm 简介
- CAShapeLayer的简介
- memcached简介及java使用方法
- NoSQL和Redis简介及Redis在Windows下的安装和使用教程
- Python的垃圾回收机制(一)之简介
- 数据为空的时候不错的代理第三方简介
- git rebase简介(基本篇)
- TCP/IP协议族(一) HTTP简介、请求方法与响应状态码
- 嵌入式(一)STM32简介
- Hibernate3.0Beta包简介
- Socket 编程简介
- 华天动力协同办公系统V380中小企业版功能简介