网路基础篇-多线程

2016-05-03  本文已影响0人  nickNameDC

开启多线程主要的三种方法NSThread、GCD、 NSOperation。

1.NSThread

1> 开线程的几种方式

 先创建,后启动
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
直接启动
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
[self performSelectorInBackground:@selector(run) withObject:nil];

2> 其他用法

NSThread *current = [NSThread currentThread]; //获取当前线程
+ (NSThread *)mainThread; // 获得主线程

3> 线程间通信
performSelectorOnMainThread.....

2.GCD(重点)

1.队列的类型
2.执行任务的方法类型
3.了解队列和方法的配合使用
4.线程间通信
dispatch_async(
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   // 执行耗时的异步操作...
   dispatch_async(dispatch_get_main_queue(), ^{
       // 回到主线程,执行UI刷新操作
   });
});
5.其他用法

dispatch_once //只执行一次
dispatch_after //延迟执行
dispatch_group_async\dispatch_group_notify

3.NSOperation

1. 基本使用

NSInvocationOperation
NSBlockOperation

2.NSOperationQueue(重点)
3. 自定义Operation(了解基本流程)
上一篇 下一篇

猜你喜欢

热点阅读