在我的"didFinishLaunchingWithOptions“方法中,我在图形用户界面中创建了一个UIProgressView &之后,我调用一个方法来调用带有NSURLConnection的WebService,以获得带有SOAP消息的NSURLConnection。
在委托方法"connectionDidFinishLoading“中,我在另一个类中使用NSXMLParser解析XML。
问题是,我想在解析XML时更新我的UIProgressView,但它是在解析完整个XML之后更新的。我听说这是因为NSURLconnection在主线程上运行,并且阻塞了UI。
如何同时解析和更新进度条?
- (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];
}发布于 2011-11-02 16:35:08
嘿,你可以更新进度条
-(void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data { //update progress bar here
}
发布于 2012-07-04 03:27:46
在加载数据时,NSURLConnection不会阻塞主线程,但您在connectionDidFinishLoading:中执行的所有操作都会阻塞主线程。如果您知道文档中可能有多少元素,则可以使用NSXMLParserDelegate回调:- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict来保存所见元素的运行总数,“完成百分比”将是该数字除以元素总数。在该回调中,您可以更新进度条。
如果不知道文档的先验长度,就很难估计它处理了多长时间,除非您保持已处理字节的运行总数。
https://stackoverflow.com/questions/7977494
复制相似问题