首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLConnection和UIProgressView

NSURLConnection和UIProgressView
EN

Stack Overflow用户
提问于 2011-11-02 16:29:12
回答 2查看 773关注 0票数 1

在我的"didFinishLaunchingWithOptions“方法中,我在图形用户界面中创建了一个UIProgressView &之后,我调用一个方法来调用带有NSURLConnection的WebService,以获得带有SOAP消息的NSURLConnection。

在委托方法"connectionDidFinishLoading“中,我在另一个类中使用NSXMLParser解析XML。

问题是,我想在解析XML时更新我的UIProgressView,但它是在解析完整个XML之后更新的。我听说这是因为NSURLconnection在主线程上运行,并且阻塞了UI。

如何同时解析和更新进度条?

代码语言:javascript
复制
- (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];
}
EN

回答 2

Stack Overflow用户

发布于 2011-11-02 16:35:08

嘿,你可以更新进度条

-(void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data { //update progress bar here

}

票数 1
EN

Stack Overflow用户

发布于 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来保存所见元素的运行总数,“完成百分比”将是该数字除以元素总数。在该回调中,您可以更新进度条。

如果不知道文档的先验长度,就很难估计它处理了多长时间,除非您保持已处理字节的运行总数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7977494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档