IT技术网www.itjs.cn

当前位置:首页 > 开发设计 > IOS > iOS开发中GCD在多线程方面的理解

iOS开发中GCD在多线程方面的理解

发布时间:2014-11-26 00:00 来源:wx0123的博客

GCD为Grand Central Dispatch的缩写。Grand Central Dispatch (GCD)是Apple开发的一个多核编程的较新的解决方法。在Mac OS X 10.6雪豹中首次推出,并在最近引入到了iOS4.0。GCD是一个替代诸如NSThread等技术的很高效和强大的技术。GCD完全可以处理诸如 数据锁定和资源泄漏等复杂的异步编程问题。

GCD可以完成很多事情,但是这里仅关注在iOS应用中实现多线程所需的一些基础知识。在开始之前,需要理解是要提供给GCD队列的是代码块,用于在系统或者用户创建的的队列上调度运行。

声明一个队列

如下会返回一个用户创建的队列:dispatch_queue_t myQueue = dispatch_queue_create(“com.iphonedevblog.post”, NULL);其中,第一个参数是标识队列的,第二个参数是用来定义队列的参数(目前不支持,因此传入NULL)。

执行一个队列  

如下会异步执行传入的代码:

dispatch_async(myQueue, ^{ [self doSomething]; });其中,首先传入之前创建的队列,然后提供由队列运行的代码块。

声明并执行一个队列

假如不需要保留要运行的队列的引用,可以通过如下代码实现之前的功能: dispatch_async(dispatch_queue_create (“com.iphonedevblog.post”, NULL), ^{ [self doSomething]; });假如需要暂停一个队列,可以调用如下代码。暂停一个队列会阻止和该队列相关的所有代码运行。dispatch_suspend(myQueue);暂停一个队列  

假如暂停一个队列不要忘记恢复。暂停和恢复的操作和内存管理中的retain和release类似。调用dispatch_suspend会增加暂 停计数,而dispatch_resume则会减少。队列只有在暂停计数变成零的情况下才开始运行。dispatch_resume(myQueue);恢复一个队列  从队列中在主线程运行代码 有 些操作无法在异步队列运行,因此必须在主线程(每个应用都有一个)上运行。UI绘图以及任何对NSNotificationCenter的调用必须在主线 程长进行。在另一个队列中访问主线程并运行代码的示例如下:dispatch_sync(dispatch_get_main_queue(), ^{ [self dismissLoginWindow]; });注意,dispatch_suspend (以及dispatch_resume)在主线程上不起作用。

使用GCD,可以让你的程序不会失去响应. 多线程不容易使用,用了GCD,会让它变得简单。你无需专门进行线程管理, 很棒!

让你的程序保持响应的原则:

1. 不要柱塞主线程

2. 把工作一到其他线程中做。

3. 做完后更新主线程的UI.

举例说明:

没有GCD的代码:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url { 

  // 在主线程调用。 

  DTweet *tw = [[DTweet alloc] initWithMsg:msg]; 

  [tweets addTweet:tw display:YES]; 

  tw.img = [imageCache getImgFromURL:url];//bottle neck 

  [tweets updateTweet:tw display:YES]; 

  [tw release]; 

}

有GCD的代码:

- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url { 
//在主线程调用。 

  DTweet *tw = [[DTweet alloc] initWithMsg:msg]; 

  [tweets addTweet:tw display:YES]; 

  dispatch_async(image_queue, ^{ 

    tw.img = [imageCache getImgFromURL:url];//放到一个异步队列里。 

    dispatch_async(main_queue, ^{ 

      [tweets updateTweet:tw display:YES];//放到异步的主线程里。 

    }); 

  }); 

  [tw release]; 

} 

1. GCD is part of libSystem.dylib 

2. #include <dispatch/dispatch.h>

NSThread的方法:代码如下:

- (void)viewDidLoad 

{ 

    [super viewDidLoad]; 

NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(print1) object:nil]; 

    [thread1 start]; 

    NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(print2) object:nil]; 

    [thread2 start]; 

} 

-(void)print1{ 

    for (int i=0; i<100; i++) { 

        NSLog(@"我是print1正在执行%d",i); 

    } 

} 

-(void)print2{ 

    for (int i=0; i<100; i++) { 

        NSLog(@"print2正在执行%d",i); 

    } 

}

NSInvocationOperation 的方法:代码如下

//    NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print1) object:@"1"]; 

//    NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print2) object:@"2"];//当然这里可以用一个方法。 

//    NSOperationQueue *queue=[[NSOperationQueue alloc]init]; 

//    [queue addOperation:operation1]; 

//    [queue addOperation:operation2];

GCD的方法:代码如下:

dispatch_queue_t t1=dispatch_queue_create("1", NULL); 

   dispatch_queue_t t2=dispatch_queue_create("2", NULL); 

   dispatch_async(t1, ^{ 

       [self print1]; 

   }); 

   dispatch_async(t2, ^{ 

       [self print2]; 

   });

Push的原理:

iOS开发中GCD在多线程方面的理解

图中,Provider是指某个iPhone软件的Push服务器,该文我将使用.net作为Provider。 APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器。

上图可以分为三个阶段。

第一阶段:.net应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。 第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。 第三阶段:iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

http://blog.csdn.net/zhuqilin0/article/details/6527113 //消息推送机制 看内存泄露时候:在搜索中搜索run 找到Run Static Snalyzer . GCD1.zip (23.1 KB)