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

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

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的代码:

 

  1. - (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url { 
  2.  
  3.   // 在主线程调用。 
  4.  
  5.   DTweet *tw = [[DTweet alloc] initWithMsg:msg]; 
  6.  
  7.   [tweets addTweet:tw display:YES]; 
  8.  
  9.   tw.img = [imageCache getImgFromURL:url];//bottle neck 
  10.  
  11.   [tweets updateTweet:tw display:YES]; 
  12.  
  13.   [tw release]; 
  14.  

有GCD的代码:

 

 

  1. - (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url { 
  2. //在主线程调用。 
  3.  
  4.   DTweet *tw = [[DTweet alloc] initWithMsg:msg]; 
  5.  
  6.   [tweets addTweet:tw display:YES]; 
  7.  
  8.   dispatch_async(image_queue, ^{ 
  9.  
  10.     tw.img = [imageCache getImgFromURL:url];//放到一个异步队列里。 
  11.  
  12.     dispatch_async(main_queue, ^{ 
  13.  
  14.       [tweets updateTweet:tw display:YES];//放到异步的主线程里。 
  15.  
  16.     }); 
  17.  
  18.   }); 
  19.  
  20.   [tw release]; 
  21.  
  22.  
  23.   
  24. 1. GCD is part of libSystem.dylib 
  25.  
  26. 2. #include <dispatch/dispatch.h> 

一》NSThread的方法:代码如下:

 

  1. - (void)viewDidLoad 
  2.  
  3.  
  4.     [super viewDidLoad]; 
  5.  
  6. NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(print1) object:nil]; 
  7.  
  8.     [thread1 start]; 
  9.  
  10.   
  11.  
  12.     NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(print2) object:nil]; 
  13.  
  14.     [thread2 start]; 
  15.  
  16.   
  17.  
  18.  
  19. -(void)print1{ 
  20.  
  21.     for (int i=0; i<100; i++) { 
  22.  
  23.         NSLog(@"我是print1正在执行%d",i); 
  24.  
  25.     } 
  26.  
  27.  
  28. -(void)print2{ 
  29.  
  30.     for (int i=0; i<100; i++) { 
  31.  
  32.         NSLog(@"print2正在执行%d",i); 
  33.  
  34.     } 
  35.  
  36. 二》 
  37.  
  38. NSInvocationOperation 
  39. 的方法:代码如下 
  40.   
  41.  
  42. //    NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print1) object:@"1"]; 
  43.  
  44. //    NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print2) object:@"2"];//当然这里可以用一个方法。 
  45.  
  46. //    NSOperationQueue *queue=[[NSOperationQueue alloc]init]; 
  47.  
  48. //    [queue addOperation:operation1]; 
  49.  
  50. //    [queue addOperation:operation2]; 
  51.  

GCD的方法:代码如下:

 

  1. dispatch_queue_t t1=dispatch_queue_create("1", NULL); 
  2.  
  3.    dispatch_queue_t t2=dispatch_queue_create("2", NULL); 
  4.  
  5.    dispatch_async(t1, ^{ 
  6.  
  7.        [self print1]; 
  8.  
  9.    }); 
  10.  
  11.    dispatch_async(t2, ^{ 
  12.  
  13.        [self print2]; 
  14.  
  15.    }); 

Push的原理:

图中,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 .

本文链接:http://www.cnblogs.com/lovewx/archive/2014/07/21/3853088.html

责任编辑:chenqingxiang 来源: cnblogs
相关推荐

2015-07-22 09:39:38

IOS多线程同步

2015-07-22 09:51:51

iOS开发线程

2018-04-11 10:51:25

多线程进程主线程

2011-01-19 15:51:41

PHPjavaweb

2024-03-14 06:51:22

GenAI人工智能

2009-01-20 09:12:16

PHPJava数据库

2011-08-18 17:07:23

IOS开发多线程NSInvocatio

2010-07-30 13:06:22

NFS端口

2009-08-20 18:11:08

C#异步委托

2011-07-12 10:15:05

2011-07-11 10:43:48

2010-07-07 18:00:43

SNMP协议

2013-03-27 10:32:53

iOS多线程原理runloop介绍GCD

2012-05-14 17:09:05

iOS

2013-07-16 10:12:14

iOS多线程多线程概念多线程入门

2016-04-12 09:48:24

nsthread多线程ios

2015-07-06 09:46:21

AeroGearAndroid推送

2011-04-25 17:27:38

投影仪

2011-08-02 10:26:59

iOS 多线程 线程

2011-08-08 13:50:29

iPhone开发 NSOperatio 多线程
点赞
收藏

51CTO技术栈公众号