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