NSOperation和NSOperationQueue


最近在网上闲逛,看到一个老外的博客,感觉里面iphone开表的文章相当不错,自己就暂且选了一篇进行翻译,以备日后使用。这里有这个博客的链接,有兴趣的可以看下(http://www.cimgf.com/)。下面是Cocoa Tutorial: NSOperation and NSOperationQueue这篇博文的原文翻译。

    在任何语言中线程都是困难的。更糟糕的是,当线程出错的时候,会出现意想不到的情况。因为这些,程序员要么完全的避免线程,要么就花费大量的时间确保线程万无一失。

    值得庆幸的是,苹果在狮子系统中对此做了大量的改进。利用NSThread封装了大量的方法,以便于线程容易管理。另外,苹果介绍了两个新的对象:NSOperation和NSOperationQueue。这个教程,我将利用一些简单的例子,展示如何使用这些新对象和怎么使多线程应用程序顺利执行。

    下面的网址你可以获得工程的例子:http://www.cimgf.com/wp-content/uploads/2008/02/asyncdownloader.zip

     在这个教程中,我将展示利用NSOperation和NSOperationQueue怎么来控制后台的线程。这个教程的目的仅仅是用来演示这些类怎么使用的,所以并不是使用这些类的唯一方法。

    如果你熟悉java语言的话。NSOperation对象很像java.lang.Runnable这个接口。就像java.lang.Runnable一样,NSOperation对象也被设计成可以扩展的。在java的Runnable中,有一个最低级的重载的方法。在NSOperation中此方法是 -(void)main。使用NSOperation最简单的方法是放置它到NSOperationQueue中。只要该操作被导入到队列中,此队列就立刻取得该操作,然后执行它。然后此操作完成,队列就释放它。

    NSOperation 的例子

    这个例子中,我写了一个NSOperation来抓取一个网页的字符串,然后把这些字符串放到NSXMLDocument中解析,然后在此操作完成之前,传递NSXMLDocument对象给主线程。

[plain]  view plain copy
  1. PageLoadOperation.h  
  2.   
  3. #import <Cocoa/Cocoa.h>  
  4.    
  5.    
  6. @interface PageLoadOperation : NSOperation {  
  7.     NSURL *targetURL;  
  8. }  
  9.    
  10. @property(retain) NSURL *targetURL;  
  11.    
  12. - (id)initWithURL:(NSURL*)url;  
  13.    
  14. @end   
[plain]  view plain copy
  1. #import "PageLoadOperation.h"  
  2. #import "AppDelegate.h"  
  3.    
  4. @implementation PageLoadOperation  
  5.    
  6. @synthesize targetURL;  
  7.    
  8. - (id)initWithURL:(NSURL*)url;  
  9. {  
  10.     if (![super init]) return nil;  
  11.     [self setTargetURL:url];  
  12.     return self;  
  13. }  
  14.    
  15. - (void)dealloc {  
  16.     [targetURL release], targetURL = nil;  
  17.     [super dealloc];  
  18. }  
  19.    
  20. - (void)main {  
  21.     NSString *webpageString = [[[NSString alloc] initWithContentsOfURL:[self targetURL]] autorelease];  
  22.    
  23.     NSError *error = nil;  
  24.     NSXMLDocument *document = [[NSXMLDocument alloc] initWithXMLString:webpageString   
  25.                                                               options:NSXMLDocumentTidyHTML   
  26.                                                                 error:&error];  
  27.     if (!document) {  
  28.         NSLog(@"%s Error loading document (%@): %@", _cmd, [[self targetURL] absoluteString], error);  
  29.         return;  
  30.     }     
  31.    
  32.     [[AppDelegate shared] performSelectorOnMainThread:@selector(pageLoaded:)  
  33.                                            withObject:document  
  34.                                         waitUntilDone:YES];  
  35.     [document release];  
  36. }  
  37.    
  38. @end  

    就像你看到的,这个类非常的简单。初始化的时候,它接受了一个URL,并且存储了这个URL。当main方法被调用时,它从URL中构造了一个字符串,然后传递这个字符串传给NSXMLDocument初始化。假如在装载xml文档的时候,没有错误发生,它将回传给AppDelegate,在主线程上,然后此任务完成。当main方法结束时,NSOperation也会在队列中被释放。

[plain]  view plain copy
  1. AppDelegate.h  
[plain]  view plain copy
  1. #import <Cocoa/Cocoa.h>  
  2.    
  3. @interface AppDelegate : NSObject {  
  4.     NSOperationQueue *queue;  
  5. }  
  6.    
  7. + (id)shared;  
  8. - (void)pageLoaded:(NSXMLDocument*)document;  
  9.    
  10. @end  
[plain]  view plain copy
  1. #import "AppDelegate.h"  
  2. #import "PageLoadOperation.h"  
  3.    
  4. @implementation AppDelegate  
  5. static AppDelegate *shared;  
  6. static NSArray *urlArray;  
  7.    
  8. - (id)init  
  9. {  
  10.     if (shared) {  
  11.         [self autorelease];  
  12.         return shared;  
  13.     }  
  14.     if (![super init]) return nil;  
  15.    
  16.     NSMutableArray *array = [[NSMutableArray alloc] init];  
  17.     [array addObject:@"http://www.google.com"];  
  18.     [array addObject:@"http://www.apple.com"];  
  19.     [array addObject:@"http://www.yahoo.com"];  
  20.     [array addObject:@"http://www.zarrastudios.com"];  
  21.     [array addObject:@"http://www.macosxhints.com"];  
  22.     urlArray = array;  
  23.    
  24.     queue = [[NSOperationQueue alloc] init];  
  25.     shared = self;  
  26.     return self;  
  27. }  
  28.    
  29. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification  
  30. {  
  31.     for (NSString *urlString in urlArray) {  
  32.         NSURL *url = [NSURL URLWithString:urlString];  
  33.         PageLoadOperation *plo = [[PageLoadOperation alloc] initWithURL:url];  
  34.         [queue addOperation:plo];  
  35.         [plo release];  
  36.     }  
  37. }  
  38.    
  39. - (void)dealloc  
  40. {  
  41.     [queue release], queue = nil;  
  42.     [super dealloc];  
  43. }  
  44.    
  45. + (id)shared;  
  46. {  
  47.     if (!shared) {  
  48.         [[AppDelegate alloc] init];  
  49.     }  
  50.     return shared;  
  51. }  
  52.    
  53. - (void)pageLoaded:(NSXMLDocument*)document;  
  54. {  
  55.     NSLog(@"%s Do something with the XMLDocument: %@", _cmd, document);  
  56. }  
  57.    
  58. @end  

    在这个例子的AppDelegate中,两件事正在发生。第一,在初始化方法中,NSOperationQueue装载一些URL数组。然后当应用程序完成装载的时候,也就是在被应用程序实例调用的applicationDidFinishLaunching方法中,通过url数组循环,为每个url创建一个任务,然后放置这些任务到NSOperationQueue中。只要任何一个NSOperation被安排到队列中,就回立刻被队列获取,然后分配它到一个NSThread中,然后NSThread就会运行NSOperation中的main函数中的方法。一旦操作完成,线程就报告给队列,然后队列就释放这个操作。

   NSOperationQueue同步

   在这个简单的例子中,很困那导入足够多的对象,使之并行运行。然而,如果你运行的任务需要花费大量的时间,你将会看到此队列同时运行很多任务。幸运的是,如果你想降低并发任务的数量,你能在AppDelegate的初始化方法中,
如下的设置:

[plain]  view plain copy
  1.    
  2. - (id)init  
  3. {  
  4.     if (shared) {  
  5.         [self autorelease];  
  6.         return shared;  
  7.     }  
  8.     if (![super init]) return nil;  
  9.    
  10.     NSMutableArray *array = [[NSMutableArray alloc] init];  
  11.     [array addObject:@"http://www.google.com"];  
  12.     [array addObject:@"http://www.apple.com"];  
  13.     [array addObject:@"http://www.yahoo.com"];  
  14.     [array addObject:@"http://www.zarrastudios.com"];  
  15.     [array addObject:@"http://www.macosxhints.com"];  
  16.     urlArray = array;  
  17.     queue = [[NSOperationQueue alloc] init];  
  18.     [queue setMaxConcurrentOperationCount:2];  
  19.     shared = self;  
  20.     return self;  
  21. }   

   这样的话,在相同的时间,队列就会被控制在只有2个操作在运行。剩下的操作将会等待,直到头两个操作被执行完,然后队列才能得到机会去执行其他的操作,直到队列空为止。

   总结

    这些是NSOperation和NSOperationQueue最基础的操作。你会注意到代码中很多地方都与NSOperation,NSOperationQueue的建立和使用没有关系。事实上使用NSOperation和NSOperationQueue的代码是惊人的小。然而,使用如此少的代码,你就可以在程序中使用多线程,所以它提供了一个很好的方式方便的管理应用程序中复杂的任务。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM