自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone開發(fā)中使用NSOperation實現(xiàn)異步下載

移動開發(fā) iOS
iPhone開發(fā)中使用NSOperation實現(xiàn)異步下載是本文要介紹的內(nèi)容,在iphone開發(fā)中,異步操作是一個永恒的話題,尤其當iphone手機需要和遠程服務(wù)器進行交互時,使用異步請求是很普遍的做法。

iPhone開發(fā)中使用NSOperation實現(xiàn)異步下載是本文要介紹的內(nèi)容,在iphone開發(fā)中,異步操作是一個永恒的話題,尤其當iphone手機需要和遠程服務(wù)器進行交互時,使用異步請求是很普遍的做法。

通常,這需要NSURLConnection和 NSOperation結(jié)合起來使用。這方面的資料網(wǎng)絡(luò)上自然有不少的介紹,不過要找一個能運行的代碼也并不容易。許多文章介紹的并不全面,或者使用了過 時的SDK,在新IOS版本下并不適用(當前***的ios是4.2了)。這些代碼很經(jīng)典,但仍然很容易使人誤入歧途。

本文總結(jié)了眾多文檔介紹的方法和代碼,揭示了異步操作中的實現(xiàn)細節(jié)和初學(xué)者(包括筆者)易犯的錯誤,使后來者少走彎路。

一、使用NSOperation實現(xiàn)異步請求

1、新建類,繼承自NSOperation。

  1. @interfaceURLOperation : NSOperation  
  2. {  
  3.     NSURLRequest*  _request;  
  4.     NSURLConnection* _connection;  
  5.     NSMutableData* _data;  
  6.     //構(gòu)建gb2312的encoding  
  7.     NSStringEncodingenc;  
  8. }  
  9. - (id)initWithURLString:(NSString*)url;  
  10. @property(readonly) NSData *data;  
  11. @end 

接口部分不多做介紹,我們來看實現(xiàn)部分。

首先是帶一個NSString參數(shù)的構(gòu)造函數(shù)。在其中初始化成員變量。

其中enc是NSStringEncoding類型,因為服務(wù)器返回的字符中使用了中文 ,所以我們通過它指定了一個gb2312的字符編碼。

許多資料中說,需要在NSOperation中重載一個叫做isConcurrent的函數(shù)并在其中返回YES,否則不支持異步執(zhí)行。但是實際上,我們在這里注釋了這個重載方法,程序也沒有報任何錯誤,其執(zhí)行方式依然是異步的。

  1. @implementationURLOperation  
  2. @synthesizedata=_data;  
  3. - (id)initWithURLString:(NSString*)url {  
  4.     if(self= [selfinit]) {  
  5.         NSLog(@"%@",url);  
  6.         _request= [[NSURLRequestalloc] initWithURL:[NSURLURLWithString:url  
  7.         //構(gòu)建gb2312的encoding  
  8.         enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
  9.         _data= [[NSMutableDatadata] retain];  
  10.     }  
  11.     returnself;  
  12. }  
  13. - (void)dealloc {  
  14.     [_requestrelease],_request=nil;  
  15.     [_datarelease],_data=nil;  
  16.     [_connectionrelease],_connection=nil;  
  17.     [superdealloc];  
  18. }  
  19. // 如果不重載下面的函數(shù),異步方式調(diào)用會出錯  
  20. //- (BOOL)isConcurrent {  
  21. //  return YES;//返回yes表示支持異步調(diào)用,否則為支持同步調(diào)用  
  22. //} 

整個類中最重要的方法是start方法。Start是 NSOperation類的主方法,主方法的叫法充分說明了其重要性,因為這個方法執(zhí)行完后,該NSOperation的執(zhí)行線程就結(jié)束了(返回調(diào)用者的 主線程),同時對象實例就會被釋放,也就意味著你定義的其他代碼(包括delegate方法)也不會被執(zhí)行。很多資料中的start方法都只有最簡單的一 句(包括“易飛揚的博客 “的博文):

  1. [NSURLConnection connectionWithRequest:_request delegate:self]; 

如果這樣的話,delegate方法沒有執(zhí)行機會。因為start方法結(jié)束后delegate(即self對象)已經(jīng)被釋放了,delegate的方法也就無從執(zhí)行。

所以在上面的代碼中,還有一個while循環(huán),這個while循環(huán)的退出條件是http連接終止(即請求結(jié)束)。 當循環(huán)結(jié)束,我們的工作也就完成了。

  1. // 開始處理-本類的主方法  
  2. - (void)start {  
  3.     if(![selfisCancelled]) {  
  4.         NSLog(@"start operation");  
  5.         // 以異步方式處理事件,并設(shè)置代理  
  6.         _connection=[[NSURLConnectionconnectionWithRequest:_requestdelegate:self]retain];  
  7.         //下面建立一個循環(huán)直到連接終止,使線程不離開主方法,否則connection的delegate方法不會被調(diào)用,因為主方法結(jié)束對象的生命周期即終止  
  8.         //這個問題參考http://www.cocoabuilder.com/archive/cocoa/279826-nsurlrequest-and-nsoperationqueue.html  
  9.         while(_connection!= nil) {  
  10.             [[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];     
  11.         }  
  12.     }  

接下來,是NSURLConnection的 delegate方法,這部分的代碼和大部分資料的介紹是一樣的,你可以實現(xiàn)全部的delegate方法,但這里我們只實現(xiàn)其中3個就足夠了,其余的方法 不用理會。如你所見,你可以在其中添加自己想到的任何代碼,包括接收數(shù)據(jù),進行字符編碼或者做xml解析。

  1. #pragma mark NSURLConnection delegate Method  
  2. // 接收到數(shù)據(jù)(增量)時  
  3.  
  4. - (void)connection:(NSURLConnection*)connection  
  5.     didReceiveData:(NSData*)data {  
  6.     NSLog(@"connection:");  
  7.     NSLog(@"%@",[[NSStringalloc] initWithData:data encoding:enc]);  
  8.     // 添加數(shù)據(jù)  
  9.     [_dataappendData:data];  
  10. }  
  11. // HTTP請求結(jié)束時  
  12. - (void)connectionDidFinishLoading:(NSURLConnection*)connection {  
  13.     [_connectionrelease],_connection=nil;  
  14.     //NSLog(@"%@",[[NSString alloc] initWithData:_data encoding:enc]);  
  15. }  
  16. -(void)connection: (NSURLConnection*) connection didFailWithError: (NSError*) error{  
  17.     NSLog(@"connection error");  
  18. }  
  19.  
  20. @end 

到此,雖然代碼還沒有完成,但我們已經(jīng)可以運行它了。你可以看到console輸出的內(nèi)容,觀察程序的運行狀態(tài)。

2、調(diào)用NSOperation

我們的NSOperation類可以在ViewController中調(diào)用,也可以直接放在AppDelegate中進行。

在這里,我是通過點擊按鈕來觸發(fā)調(diào)用代碼的:

  1. -(void)loginClicked{  
  2.     //構(gòu)造登錄請求url  
  3.     NSString* url=@”http://google.com”;  
  4.     _queue= [[NSOperationQueuealloc] init];  
  5.     URLOperation* operation=[[URLOperationalloc ]initWithURLString:url];  
  6.     // 開始處理  
  7.     [_queueaddOperation:operation];  
  8.     [operation release];//隊列已對其retain,可以進行release;  

_queue是一個NSOperationQueue對象,當往其中添加 NSOperation 對象后, NSOperation 線程會被自動執(zhí)行(不是立即執(zhí)行,根據(jù)調(diào)度情況)。

3、KVO編程模型

我們的NSOperation完成了向服務(wù)器的請求并將 服務(wù)器數(shù)據(jù)下載到成員變量_data中了。現(xiàn)在的問題是,由于這一切是通過異步操作進行的,我們無法取得_data中的數(shù)據(jù),因為我們不知道什么時候異步 操作完成,以便去訪問_data屬性(假設(shè)我們將_data定義為屬性了),取得服務(wù)器數(shù)據(jù)。

我們需要一種機制,當NSOperation完成所有工作之后,通知調(diào)用線程。

這里我們想到了KVO編程模型(鍵-值觀察模型)。這是cocoa綁定技術(shù)中使用的一種設(shè)計模式,它可以使一個對象在屬性值發(fā)生變化時主動通知另一個對象并觸發(fā)相應(yīng)的方法。具體請參考cocoa參考庫:

  1. http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/CocoaBindings/index.html,  
  2. 以及   
  3. http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/
  4. KVOBasics.html#//apple_ref/doc/uid/20002252  

兩篇文檔。

首先,我們在NSOperation的子類中添加一個BOOL變量,當這個變量變?yōu)閅ES時,標志異步操作已經(jīng)完成:

  1. BOOL_isFinished; 

在實現(xiàn)中加入這個變量的訪問方法:

  1. - (BOOL)isFinished  
  2. {  
  3.     return_isFinished;  

cocoa的KVO模型中,有兩種通知觀察者的方式,自動通知和手動通知。顧名思義,自動通知由cocoa在屬性值變化時自動通知觀察者,而手動通知需要在值變化時調(diào)用willChangeValueForKey:和didChangeValueForKey:方法通知調(diào)用者。 為求簡便,我們一般使用自動通知。

要使用自動通知,需要在automaticallyNotifiesObserversForKey方法中明確告訴cocoa,哪些鍵值要使用自動通知:

  1. //重新實現(xiàn)NSObject類中的automaticallyNotifiesObserversForKey:方法,返回yes表示自動通知。  
  2.  
  3. + (BOOL):(NSString*)key  
  4. {  
  5.     //當這兩個值改變時,使用自動通知已注冊過的觀察者,觀察者需要實現(xiàn)observeValueForKeyPath:ofObject:change:context:方法  
  6.     if([key isEqualToString:@"isFinished"])  
  7.     {  
  8.         returnYES;  
  9.     }  
  10.     return[superautomaticallyNotifiesObserversForKey:key];  

然后,在需要改變_isFinished變量的地方,使用

  1. [selfsetValue:[NSNumbernumberWithBool:YES] forKey:@"isFinished"]; 

方法,而不是僅僅使用簡單賦值。

我們需要在3個地方改變isFinished值為YES, 請求結(jié)束時、連接出錯誤,線程被cancel。請在對應(yīng)的方法代碼中加入上面的語句。

***,需要在觀察者的代碼中進行注冊。打開ViewController中調(diào)用NSOperation子類的地方,加入:

  1.     //kvo注冊  
  2.     [operation addObserver:selfforKeyPath:@"isFinished"  
  3.                    options:(NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld) context:operation];  
  4.                      
  5. 并實現(xiàn)observeValueForKeyPath方法:  
  6.  
  7. //接收變更通知  
  8. - (void)observeValueForKeyPath:(NSString*)keyPath  
  9.                       ofObject:(id)object  
  10.                         change:(NSDictionary*)change  
  11.                        context:(void*)context  
  12. {  
  13.     if([keyPath isEqual:@"isFinished"]) {  
  14.         BOOLisFinished=[[change objectForKey:NSKeyValueChangeNewKey] intValue];  
  15.         if(isFinished) {//如果服務(wù)器數(shù)據(jù)接收完畢  
  16.             [indicatorViewstopAnimating];  
  17.             URLOperation* ctx=(URLOperation*)context;  
  18.             NSStringEncodingenc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
  19.             NSLog(@"%@",[[NSStringalloc] initWithData:[ctx data] encoding:enc]);  
  20.             //取消kvo注冊  
  21.             [ctx removeObserver:self  
  22.                      forKeyPath:@"isFinished"];  
  23.         }        
  24.     }else{  
  25.         // be sure to call the super implementation  
  26.         // if the superclass implements it  
  27.         [superobserveValueForKeyPath:keyPath  
  28.                              ofObject:object  
  29.                                change:change  
  30.                               context:context];  
  31.     }  

運行程序,查看控制臺的輸出。

4、libxml的sax解析接口

iphone和服務(wù)器交互通常使用xml數(shù)據(jù)交換格式,因此本文中也涉及到了xml文件解析的問題。有許多有名氣的xml解析器可供我們選擇, 如: BXML,TouchXML,KissXML,TinyXML的第三方庫和GDataXML。

Xml解析分為兩類,一類是DOM解析,一類為SAX解析。前者如GDataXML,解析過程中需要建立文檔樹,操作XML元素時通過樹形結(jié)構(gòu)進行導(dǎo)航。DOM解析的特點是便于程序員理解xml文檔樹結(jié)構(gòu),API 的使用簡單;缺點是速度較SAX解析慢,且內(nèi)存開銷較大。在某些情況下, 比如iphone開發(fā),受制于有限的內(nèi)存空間(一個應(yīng)用最多可用10幾m的內(nèi)存), DOM解析無法使用(當然,在模擬器上是沒有問題的)。

libxml2的是一個開放源碼庫,默認情況下iPhone SDK 中已經(jīng)包括在內(nèi)。 它是一個基于C的API,所以在使用上比cocoa的 NSXML要麻煩許多(一種類似c函數(shù)的使用方式),但是該庫同時支持DOM和SAX解析,其解析速度較快,而且占用內(nèi)存小,是最適合使用在iphone上的解析器。 從性能上講,所有知名的解析器中,TBXML最快,但在內(nèi)存占用上,libxml使用的內(nèi)存開銷是最小的。因此,我們決定使用libxml的sax接口。

首先,我們需要在project中導(dǎo)入framework:libxml2.dylib。

雖然libxml是sdk中自帶的,但它的頭文件卻未放在默認的地方,因此還需要我們設(shè)置project的build選項:HEADER_SEARCH_PATHS = /usr/include/libxml2,否則libxml庫不可用。

然后,我們就可以在源代碼中 #import <libxml/tree.h>了。

假設(shè)我們要實現(xiàn)這樣的功能:有一個登錄按鈕,點擊后將用戶密碼帳號發(fā)送http請求到服務(wù)器(用上文中介紹的異步請求技術(shù)),服務(wù)器進行驗證后以xml文件方式返回驗證結(jié)果。我們要用libxml的sax方式將這個xml文件解析出來。

服務(wù)器返回的xml文件格式可能如下:

  1. <?xml version="1.0" encoding="GB2312" standalone="no" ?> 
  2. <root> 
  3. <login_info> 
  4. <login_status>true</login_status> 
  5. </login_info> 
  6. <List> 
  7.         <system Name=xxx Path=xxx ImageIndex=xxx> 
  8. ……  
  9. </List> 
  10. </root> 

其中有我們最關(guān)心的1個元素:login_status 。

如果login_status返回false,說明登錄驗證失敗,否則,服務(wù)器除返回login_status外,還會返回一個list元素,包含了一些用戶的數(shù)據(jù),這些數(shù)據(jù)是<system>元素的集合。

整個實現(xiàn)步驟見下。

首先,實現(xiàn)一個超類, 這個超類是一個抽象類,許多方法都只是空的,等待subclass去實現(xiàn)。

其中有3個方法與libxml的sax接口相關(guān),是sax解析過程中的3個重要事件的回調(diào)方法,分別是元素的開始標記、元素體(開始標記和結(jié)束標記之間的文本)、結(jié)束標記。Sax中有許多的事件,但絕大部分時間,我們只需要處理這3個事件。 因為很多時候,我們只會對xml文件中的元素屬性和內(nèi)容感興趣,而通過這3個事件已經(jīng)足以使我們讀取到xml節(jié)點的屬性和內(nèi)容 。

而成員變量中,_root變量是比較關(guān)鍵的,它以dictionary的形式保存了解析結(jié)果,因為任何xml文檔的根節(jié)點都是root,所以無論什么樣子的xml文件,都可以放在這個_root 中。

因此我們?yōu)?_root 變量提供了一個訪問方法getResult,等xml解析結(jié)束,可以通過這個方法訪問_root。

  1. #import <Foundation/Foundation.h> 
  2. #import <libxml/tree.h> 
  3. @interfaceBaseXmlParser : NSObject {  
  4.     NSStringEncodingenc;  
  5.     NSMutableDictionary*    _root;  
  6. }  
  7. // Property  
  8. - (void)startElementLocalName:(constxmlChar*)localname  
  9.                        prefix:(constxmlChar*)prefix  
  10.                           URI:(constxmlChar*)URI  
  11.                 nb_namespaces:(int)nb_namespaces  
  12.                    namespaces:(constxmlChar**)namespaces  
  13.                 nb_attributes:(int)nb_attributes  
  14.                  nb_defaulted:(int)nb_defaultedslo  
  15.                    attributes:(constxmlChar**)attributes;  
  16. - (void)endElementLocalName:(constxmlChar*)localname  
  17.                      prefix:(constxmlChar*)prefix URI:(constxmlChar*)URI;  
  18. - (void)charactersFound:(constxmlChar*)ch  
  19.                     len:(int)len;  
  20. -(NSDictionary*)getResult;  
  21. @end  
  22. #import "BaseXmlParser.h"  
  23. @implementationBaseXmlParser  
  24. // Property  
  25. -(id)init{  
  26.     if(self=[superinit]){  
  27.         //構(gòu)建gb2312的encoding  
  28.         enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
  29.         _root=[[NSMutableDictionaryalloc]init];  
  30.     }  
  31.     returnself;  
  32. }  
  33. -(void)dealloc{  
  34.     [_rootrelease],_root=nil;  
  35.     [superdealloc];  
  36. }  
  37. //--------------------------------------------------------------//  
  38. #pragma mark -- libxml handler,主要是3個回調(diào)方法--  
  39. //--------------------------------------------------------------//  
  40. //解析元素開始標記時觸發(fā),在這里取元素的屬性值  
  41. - (void)startElementLocalName:(constxmlChar*)localname  
  42.                        prefix:(constxmlChar*)prefix  
  43.                           URI:(constxmlChar*)URI  
  44.                 nb_namespaces:(int)nb_namespaces  
  45.                    namespaces:(constxmlChar**)namespaces  
  46.                 nb_attributes:(int)nb_attributes  
  47.                  nb_defaulted:(int)nb_defaultedslo  
  48.                    attributes:(constxmlChar**)attributes  
  49. {    
  50. }  
  51. //解析元素結(jié)束標記時觸發(fā)  
  52. - (void)endElementLocalName:(constxmlChar*)localname  
  53.                      prefix:(constxmlChar*)prefix URI:(constxmlChar*)URI  
  54. {  
  55. }  
  56. //解析元素體時觸發(fā)  
  57. - (void)charactersFound:(constxmlChar*)ch  
  58.                     len:(int)len  
  59. {  
  60. }  
  61. //返回解析結(jié)果  
  62. -(NSDictionary*)getResult{  
  63.     return_root;  
  64. }  
  65. @end 

現(xiàn)在我們需要擴展這個BaseXmlParser,并重載其中的3個sax方法。

該子類除了重載父類的3個方法外,還增加了幾個成員變 量。其中flag是一個int類型,用于sax解析的緣故,解析過程中需要合適的標志變量,用于標志當前處理到的元素標記。為了簡單起見,我們沒有為每一 個標記都設(shè)立一個標志,而是統(tǒng)一使用一個int標志,比如flag為1時,表示正在處理login_status標記,為2時,表示正在處理system 標記。

回顧前面的xml文件格式,我們其實只關(guān)心兩種標記,login_status標記和system標記。Login_status標記沒有屬性,但它的元素體是我們關(guān)心的;而system標記則相反,它并沒有元素體,但我們需要它的屬性值。

這是一個很好的例子。因為它同時展示了屬性的解析和元素體的解析。瀏覽整個類的代碼,我們總結(jié)出3個sax事件的使用規(guī)律是:

如果要讀取元素屬性,需要在“元素開始標記讀取”事件(即startElementLocalName方法)中處理;

如果要讀取元素體文本,則在“元素體讀取”事件(即charactersFound方法)中處理;

在“元素標記讀取”事件(即endElementLocalName方法)中,則進行標志變量的改變/歸零。

  1. #import <Foundation/Foundation.h> 
  2. #import <libxml/tree.h> 
  3. #import "BaseXmlParser.h"  
  4. @interfaceDLTLoginParser : BaseXmlParser {  
  5.     intflag;  
  6.     NSMutableDictionary*    _currentItem;    
  7. }  
  8. - (void)startElementLocalName:(constxmlChar*)localname  
  9.                        prefix:(constxmlChar*)prefix  
  10.                           URI:(constxmlChar*)URI  
  11.                 nb_namespaces:(int)nb_namespaces  
  12.                    namespaces:(constxmlChar**)namespaces  
  13.                 nb_attributes:(int)nb_attributes  
  14.                 nb_defaulted:(int)nb_defaultedslo  
  15.                   attributes:(constxmlChar**)attributes;  
  16. - (void):(constxmlChar*)localname  
  17.                      prefix:(constxmlChar*)prefix URI:(constxmlChar*)URI;  
  18. - (void)charactersFound:(constxmlChar*)ch  
  19.                     len:(int)len;  
  20. @end  
  21.  
  22. #import "DLTLoginParser.h"  
  23. @implementationDLTLoginParser  
  24. -(id)init{  
  25.     if(self=[superinit]){  
  26.         NSMutableArray* items=[[NSMutableArrayalloc]init];  
  27.         [_rootsetObject:items forKey:@"items"];  
  28.         [items release];//已被_root持有了,可以釋放  
  29.     }  
  30.     returnself;  
  31. }  
  32. -(void)dealloc{  
  33.     [_currentItemrelease],_currentItem=nil;  
  34.     [superdealloc];  
  35. }  
  36. //--------------------------------------------------------------//  
  37. #pragma mark -- libxml handler,主要是3個回調(diào)方法--  
  38. //--------------------------------------------------------------//  
  39. //解析元素開始標記時觸發(fā),在這里取元素的屬性值  
  40. - (void)startElementLocalName:(constxmlChar*)localname  
  41.                        prefix:(constxmlChar*)prefix  
  42.                           URI:(constxmlChar*)URI  
  43.                 nb_namespaces:(int)nb_namespaces  
  44.                    namespaces:(constxmlChar**)namespaces  
  45.                 nb_attributes:(int)nb_attributes  
  46.                  nb_defaulted:(int)nb_defaultedslo  
  47.                    attributes:(constxmlChar**)attributes  
  48. {  
  49.     // login_status,置標志為1  
  50.     if(strncmp((char*)localname, "login_status", sizeof("login_status")) == 0) {  
  51.         flag=1;  
  52.         return;  
  53.     }  
  54.     // system,置標志為2  
  55.     if(strncmp((char*)localname, "system", sizeof("system")) == 0) {  
  56.         flag=2;  
  57.         _currentItem= [NSMutableDictionarydictionary];  
  58.         //查找屬性  
  59.         NSString*key,*val;  
  60.         for(inti=0; i<nb_attributes; i++){  
  61.             key = [NSStringstringWithCString:(constchar*)attributes[0] encoding:NSUTF8StringEncoding];  
  62.             val = [[NSStringalloc] initWithBytes:(constvoid*)attributes[3] length:(attributes[4] - attributes[3]) encoding:NSUTF8StringEncoding];  
  63.             NSLog(@"key=%@,val=%@",key,val);  
  64.             if([@"Name"isEqualToString:key]) {  
  65.                 [_currentItemsetObject:val forKey:@"name"];  
  66.                 break;  
  67.             }  
  68.             // [val release];  
  69.             attributes += 5;//指針移動5個字符串,到下一個屬性  
  70.         }  
  71.         [[_rootobjectForKey:@"items"] addObject:_currentItem];  
  72.         return;  
  73.     }  

//解析元素結(jié)束標記時觸發(fā)

  1. - (void)endElementLocalName:(constxmlChar*)localname  
  2.                      prefix:(constxmlChar*)prefix URI:(constxmlChar*)URI  
  3. {  
  4.     flag=0;//標志歸零  

//解析元素體時觸發(fā)

  1. - (void)charactersFound:(constxmlChar*)ch  
  2.                     len:(int)len  
  3. {  
  4.     // 取login_status元素體  
  5.     if(flag==1) {  
  6.         NSString*   string;  
  7.         string = [[NSStringalloc] initWithBytes:ch length:len encoding:NSUTF8StringEncoding];  
  8.         [_rootsetObject:string forKey:@"login_status"];  
  9.         NSLog(@"login_status:%@",string);  
  10.     }  
  11. }  
  12.  
  13. @end 

接下來,改造我們的異步請求操作類URLOperation。首先在interface中增加

兩個變量:

  1. xmlParserCtxtPtr  _parserContext;//Xml解析器指針  
  2. BaseXmlParser* baseParser;//Xml解析器 

其中第1個變量(一個結(jié)構(gòu)體)的聲明顯得有點奇怪,似乎是跟第2個變量混淆了。這是因為libxml是一個c函數(shù)庫,其函數(shù)調(diào)用仍然使用一種面向結(jié)構(gòu)的編程風(fēng)格。所以我們在后面還會看到一些結(jié)構(gòu)體似的變量。

另外,把_data成員的類型從NSMutableData改變?yōu)镹SMutableDictionary,并把它配置為屬性 ,因為我們的請求結(jié)果應(yīng)當被xml解析器解析為dictionary了:

  1. @property(nonatomic,retain) NSDictionary *data; 

當然,記住為它提供訪問方法:

  1. @synthesizedata=_data

然后,更改initWithURLString構(gòu)造方法,為其增加一個名為xmlParser的參數(shù):

  1. - (id)initWithURLString:(NSString*)url xmlParser:(BaseXmlParser*)parser{  
  2.     if(self= [superinit]) {  
  3.         baseParser=[parser retain];  
  4.         NSLog(@"%@",url);  
  5.         _request= [[NSURLRequestalloc] initWithURL:[NSURLURLWithString:url]];//[[NSURLRequest requestWithURL:[NSURL URLWithString:url]]retain];  
  6.         //構(gòu)建gb2312的encoding  
  7.        enc=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
  8.         _data= [[NSMutableDatadata] retain];  
  9.     }  
  10.     returnself;  

在start方法中,我們可以這樣創(chuàng)建一個xml解析器指針:

  1. // 創(chuàng)建XML解析器指針  
  2. _parserContextxmlCreatePushParserCtxt(&_saxHandlerStruct, baseParser, NULL, 0, NULL);  
  3.           
  4. 注意第2個參數(shù)就是具體實現(xiàn)了sax解析的xml解析器。這個解析器對象是通過構(gòu)造函數(shù)“注入”的。  
  5.  
  6. 而***個參數(shù)是一個結(jié)構(gòu)體指針xmlSAXHandler 結(jié)構(gòu)體,這個結(jié)構(gòu)體我們定義為靜態(tài)變量(注意把定義放在@implementation⋯⋯@end之外):  
  7.  
  8. //libxml的xmlSAXHandler結(jié)構(gòu)體定義,凡是要實現(xiàn)的handler函數(shù)都寫在這里,不準備實現(xiàn)的用null代替。一般而言,我們只實現(xiàn)其中3個就夠了  
  9.  
  10. staticxmlSAXHandler _saxHandlerStruct = {  
  11.     NULL,            /* internalSubset */  
  12.     NULL,            /* isStandalone   */  
  13.     NULL,            /* hasInternalSubset */  
  14.     NULL,            /* hasExternalSubset */  
  15.     NULL,            /* resolveEntity */  
  16.     NULL,            /* getEntity */  
  17.     NULL,            /* entityDecl */  
  18.     NULL,            /* notationDecl */  
  19.     NULL,            /* attributeDecl */  
  20.     NULL,            /* elementDecl */  
  21.     NULL,            /* unparsedEntityDecl */  
  22.     NULL,            /* setDocumentLocator */  
  23.     NULL,            /* startDocument */  
  24.     NULL,            /* endDocument */  
  25.     NULL,            /* startElement*/  
  26.     NULL,            /* endElement */  
  27.     NULL,            /* reference */  
  28.     charactersFoundHandler, /* characters */  
  29.     NULL,            /* ignorableWhitespace */  
  30.     NULL,            /* processingInstruction */  
  31.     NULL,            /* comment */  
  32.     NULL,            /* warning */  
  33.     NULL,            /* error */  
  34.     NULL,            /* fatalError //: unused error() get all the errors */  
  35.     NULL,            /* getParameterEntity */  
  36.     NULL,            /* cdataBlock */  
  37.     NULL,            /* externalSubset */  
  38.     XML_SAX2_MAGIC,  /* initialized 特殊常量,照寫*/  
  39.     NULL,            /* private */  
  40.     startElementHandler,    /* startElementNs */  
  41.     endElementHandler,      /* endElementNs */  
  42.     NULL,            /* serror */  
  43. }; 

機構(gòu)體中填入了我們準備實現(xiàn)的3個方法句柄,因此我們還應(yīng)當定義這3個方法。由于結(jié)構(gòu)體是靜態(tài)的,只能訪問靜態(tài)成員,所以這3個方法也是靜態(tài)的:

  1. //3個靜態(tài)方法的實現(xiàn),其實是調(diào)用了參數(shù)ctx的成員方法,ctx在_parserContext初始化時傳入  
  2. staticvoidstartElementHandler(  
  3.                                 void* ctx,  
  4.                                 constxmlChar* localname,  
  5.                                 constxmlChar* prefix,  
  6.                                 constxmlChar* URI,  
  7.                                 intnb_namespaces,  
  8.                                 constxmlChar** namespaces,  
  9.                                 intnb_attributes,  
  10.                                 intnb_defaulted,  
  11.                                 constxmlChar** attributes)  
  12. {  
  13.     [(BaseXmlParser*)ctx  
  14.      startElementLocalName:localname  
  15.      prefix:prefix URI:URI  
  16.      nb_namespaces:nb_namespaces  
  17.      namespaces:namespaces  
  18.      nb_attributes:nb_attributes  
  19.      nb_defaulted:nb_defaulted  
  20.      attributes:attributes];  
  21. }  
  22.  
  23. staticvoidendElementHandler(  
  24.                               void* ctx,  
  25.                               constxmlChar* localname,  
  26.                               constxmlChar* prefix,  
  27.                               constxmlChar* URI)  
  28. {  
  29.     [(BaseXmlParser*)ctx  
  30.      endElementLocalName:localname  
  31.      prefix:prefix  
  32.      URI:URI];  
  33. }  
  34.  
  35. staticvoidcharactersFoundHandler(  
  36.                                    void* ctx,  
  37.                                    constxmlChar* ch,  
  38.                                    intlen)  
  39. {  
  40.     [(BaseXmlParser*)ctx  
  41.      charactersFound:ch len:len];  

其實這3個靜態(tài)方法只是調(diào)用了超類BaseXmlParser的成員方法, 他的具體類型依賴于ctx的注入類型, 也就是說,這里的ctx可以是任何BaseXmlParser的子類。 實際使用中,我們應(yīng)該注入其子類, 從而可以根據(jù)不同的情況為URLOperation“注入”不同的解析器,實現(xiàn)解析不同的xml文件的目的 。

現(xiàn)在,需要把解析器應(yīng)用到NSURLConnection的委托方法中(這里省略了部分代碼,只列出了新增加的部分):

  1. #pragma mark NSURLConnection delegate Method  
  2.  
  3. // 接收到數(shù)據(jù)(增量)時  
  4. - (void)connection:(NSURLConnection*)connection  
  5.     didReceiveData:(NSData*)data{  
  6.     // 使用libxml解析器進行xml解析  
  7.     xmlParseChunk(_parserContext, (constchar*)[databytes], [datalength], 0);  
  8.          ⋯⋯  
  9. }  
  10.  
  11. // HTTP請求結(jié)束時  
  12.  
  13. - (void)connectionDidFinishLoading:(NSURLConnection*)connection {  
  14.              if(baseParser!=nil&& baseParser!=NULL){  
  15.         [selfsetData:[[NSDictionaryalloc] initWithDictionary:[baseParsergetResult]]];  
  16.     }else{  
  17.         NSLog(@"baseparser is nil");  
  18.     }  
  19.     // 添加解析數(shù)據(jù)(結(jié)束),注意***一個參數(shù)termindate  
  20.  
  21.     xmlParseChunk(_parserContext, NULL, 0, 1);  
  22.  
  23.    
  24.  
  25.     // 釋放XML解析器  
  26.     if(_parserContext) {  
  27.         xmlFreeParserCtxt(_parserContext), _parserContextNULL;  
  28.     }  
  29.  
  30. ⋯⋯  
  31. }  
  32.  
  33. -(void)connection: (NSURLConnection*) connection didFailWithError: (NSError*) error{  
  34.  
  35.     // 釋放XML解析器  
  36.     if(_parserContext) {  
  37.         xmlFreeParserCtxt(_parserContext), _parserContextNULL;  
  38.     }  
  39. }  
  40. @end 

接下來,在“登錄”按鈕中代碼也要做相應(yīng)的修改,因為URLOperation的構(gòu)造函數(shù)要求傳遞一個具體的xml解析器對象:

  1. //構(gòu)造xmlparser  
  2. DLTLoginParser* parser=[[DLTLoginParseralloc]init];  
  3. URLOperation* operation=[[URLOperationalloc ]initWithURLString:url xmlParser:parser];  
  4. [parser release]; 

然后,在接收變更通知方法中打印解析結(jié)果:

  1. URLOperation* ctx=(URLOperation*)context;  
  2. NSLog(@"%@",[ctx data]); 

后臺打印結(jié)果:

  1. {  
  2.     items =     (  
  3.                 {  
  4.             name = "/U4e91/U7535/U4f01/U4fe1/U901a";  
  5.         },  
  6.                 {  
  7.             name = "/U79fb/U52a8/U8c03/U5ea6";  
  8.         },  
  9.                 {  
  10.             name = "/U79fb/U52a8/U62a2/U4fee";  
  11.         }  
  12.     );  
  13.     "login_status" = true;  

小結(jié):iPhone開發(fā)中使用NSOperation實現(xiàn)異步下載的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 論壇
相關(guān)推薦

2011-08-08 13:50:29

iPhone開發(fā) NSOperatio 多線程

2011-08-15 15:26:20

iPhone開發(fā)CocoaXML

2011-07-20 14:53:28

iPhone NSLocalize 國際化

2011-08-08 10:42:46

iPhone UITableVie 分頁

2011-08-11 13:26:30

iPhoneNSLocalized

2011-08-17 13:27:08

iPhone游戲開發(fā)objective-c

2021-03-22 08:45:30

異步編程Java

2011-08-17 14:57:31

iPhone應(yīng)用視頻播放

2015-06-16 11:06:42

JavaCompletable

2024-02-07 11:44:20

NestJSRxJS異步編程

2013-07-15 15:12:40

iOS多線程NSOperationNSOperation

2009-02-24 11:05:07

ibmdwiphonegoogle

2021-01-19 05:30:55

C# 8異步流IEnumerable

2010-10-18 13:16:24

GalleryAndroid

2011-07-08 15:08:16

iPhone 圖片

2011-07-26 14:18:20

2011-08-15 15:44:46

iPhone開發(fā)PDF

2011-08-18 16:24:44

iPhone開發(fā)圖片

2011-07-27 11:19:33

iPhone UITableVie

2011-07-28 10:11:54

iPhone開發(fā) 備忘
點贊
收藏

51CTO技術(shù)棧公眾號