Does [NSXMLParser initwithcontentsofurl:…] lock while loading?

梦想的初衷 提交于 2020-01-06 01:56:09

问题


I'm loading a rather simple XML file from a URL. I wanted to show the network activity indicator while the parser is loading the file, but obviously not while it is parsing.

Question: Does the initWithContentsOfUrl: method lock program execution while the document is loaded from the url? In other words, is the code below correct?

It seems obvious to me that this is okay, but I wanted to make 100% sure.

NSString* const urlString = @"...";
NSURL* url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];   

回答1:


This article explains it pretty well http://akosma.com/2010/05/28/initwithcontentsofurl-methods-considered-harmful/:

The main problem with these methods, of course, is the fact that they are synchronous; this means that the thread executing them (usually the UI thread) will block completely until they return, and in most applications this means that you are de-facto blocking the whole application for an unknown amount of time. This means that no buttons or UI widgets will react to input, no navigation will be possible, no touch events will be delivered or executed, nothing will happen at all until the network operation completes.

Even worse; when using initWithContentsOfURL:, there is no timeout, there is no meaningful feedback for network failures, and no way for the user to cancel the current network operation. This last factor justifies by itself not using initWithContentsOfURL: at all; you must never ship code that leads to a bad user experience. Your users will resent this and will complain!




回答2:


If you want to display download progress, you will need to download the file yourself using something like NSURLConnection, then pass the local file path to the XML parser.




回答3:


Yes, it does block. So your activity indocator won't be displayed.



来源:https://stackoverflow.com/questions/10961270/does-nsxmlparser-initwithcontentsofurl-lock-while-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!