NSOperationQueue和NSOperation的使用方法

移动开发 iOS
多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。

首先是建立NSOperationQueue和NSOperations。NSOperationQueue会建立一个线程管理器,每个加入到线程operation会有序的执行。

  1. NSOperationQueue *queue = [NSOperationQueue new];    
  2. NSInvocationOperation *operation = [[NSInvocationOperation alloc];    
  3. initWithTarget:self    
  4. selector:@selector(doWork:)    
  5. object:someObject];    
  6. [queue addObject:operation];    
  7. [operation release];    

使用NSOperationQueue的过程:   

1.  建立一个NSOperationQueue的对象

2.  建立一个NSOperation的对象

3.  将operation加入到NSOperationQueue中

4.  release掉operation

NSInvocationOperation,NSInvocationOperation是NSOperation的子类,允许运行在operation中的targer和selector

多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。

本次介绍NSOperation的子集,简易方法的NSInvocationOperation:

  1. @implementation MyCustomClass    
  2. - (void)launchTaskWithData:(id)data    
  3. {    
  4.     //创建一个NSInvocationOperation对象,并初始化到方法    
  5.     //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method)    
  6.     //在这里,object后的值是想传递给前面方法的数据    
  7.     NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self    
  8.                     selector:@selector(myTaskMethod:) object:data];    
  9.     // 下面将我们建立的操作“Operation”加入到本地程序的共享队列中(加入后方法就会立刻被执行)    
  10.     // 更多的时候是由我们自己建立“操作”队列    
  11.     [[MyAppDelegate sharedOperationQueue] addOperation:theOp];    
  12. }    
  13. // 这个是真正运行在另外一个线程的“方法”    
  14. - (void)myTaskMethod:(id)data    
  15. {    
  16.     // Perform the task.    
  17. }    
  18. @end   

一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。下面是建立并初始化一个操作队列:

  1. @interface MyViewController : UIViewController {    
  2.     NSOperationQueue *operationQueue;    
  3.     //在头文件中声明该队列    
  4. }    
  5. @end    
  6. @implementation MyViewController    
  7. - (id)init    
  8. {    
  9.     self = [super init];    
  10.     if (self) {    
  11.         operationQueue = [[NSOperationQueue alloc] init]; //初始化操作队列    
  12.         [operationQueue setMaxConcurrentOperationCount:1];    
  13.         //在这里限定了该队列只同时运行一个线程    
  14.         //这个队列已经可以使用了    
  15.     }    
  16.     return self;    
  17. }    
  18. - (void)dealloc    
  19. {    
  20.     [operationQueue release];    
  21.     //正如Alan经常说的,我们是程序的好公民,需要释放内存!    
  22.     [super dealloc];    
  23. }    
  24. @end  

简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。

责任编辑:闫佳明 来源: iteye
相关推荐

2011-04-08 10:43:44

2011-09-07 10:36:58

ubuntuUbuntuOne

2018-06-20 10:34:56

堆栈iOSswift

2012-01-13 09:55:54

jQuery

2011-02-24 13:09:10

FireFTP

2010-02-22 18:46:31

2009-08-21 18:00:38

ASP.NET mac

2014-05-06 10:20:02

2011-05-20 17:05:59

ADO.NET

2010-05-04 14:02:53

Oracle同义词

2012-03-06 10:17:45

iOS SQLite3iOSSQLite3

2023-02-08 08:40:21

2009-12-02 16:04:44

PHP fsockop

2010-01-06 10:18:02

JSON类

2009-12-24 16:36:06

WPF InkCanv

2010-10-08 16:01:17

mysql UPDAT

2010-05-18 14:06:22

SubVersion和

2011-07-20 14:45:43

C++结构体

2009-12-31 16:04:39

ADO.NET技术

2010-08-09 10:16:01

FlexBuilder
点赞
收藏

51CTO技术栈公众号