2011-11-02 31 views
1

在我的「didFinishLaunchingWithOptions」方法中,我在我的GUI &中創建了一個UIProgressView,之後我調用一個方法來調用具有NSURLConnection的WebService以獲取帶有SOAP消息的XML。NSURLConnection&UIProgressView

在委託方法「connectionDidFinishLoading」中,我使用另一個類中的NSXMLParser解析XML。

問題是我想在解析XML時更新我的​​UIProgressView,但是在解析完整個XML後更新它。我聽說這是因爲NSURLconnection在主線程上運行並阻塞了用戶界面。

如何解析並更新進度欄?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

NSString * theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 

NSLog(@"The XML : %@", theXML); 

[theXML release]; 

theXmlParser = [[NSXMLParser alloc] initWithData:webData]; 

XMLParser * theParseur = [[XMLParser alloc] initXMLParser]; 

[theXmlParser setDelegate:theParseur]; 

[theXmlParser setShouldProcessNamespaces:NO]; 
[theXmlParser setShouldReportNamespacePrefixes:NO]; 
[theXmlParser setShouldResolveExternalEntities:NO]; 

NSLog(@"Begin parsing..."); 

BOOL success = [theXmlParser parse]; 

if(success) { 

    // my code... 

} else { 

    NSLog(@"XML partner : End Parsing > ERROR : %@", [[theXmlParser parserError] localizedDescription]); 

    [theXmlParser release]; 
} 

[connection release]; 
[webData release]; 
} 

回答

1

嘿,你可以在
-(void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data { //update progress bar here
}

+0

謝謝您的回覆!但我不想在接收XML時更新進度欄,但在「connectionDidFinishLoading」中解析它時。你明白嗎 ? – Lelka

+0

ok..parsing ....因爲你必須在不同的點更新進度條...共享你的代碼已收到響應.... –

+0

- (void)連接:(NSURLConnection *)連接didReceiveResponse:(NSURLResponse *)響應{ \t \t [webData setLength:0]; }(SOrry,我不知道如何包裝我的代碼以獲得更好的閱讀效果...) – Lelka

0

NSURLConnection更新進度條不阻止當加載數據的主線程,但你在connectionDidFinishLoading:意志做了所有的東西。如果您知道文檔中可能有多少元素,則可以使用NSXMLParserDelegate回調:- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict來保持可見元素的運行總數,「完成百分比」將是該數字除以元素的總數。在那個回調中,你可以更新你的進度條。

不知道事先知道文檔的長度,很難估計它在處理中的距離,除非你保持總處理字節數。