How to handle freezing programm at loading xml from bad url?


I want to handle freezing my program, when it load an xml from bad address. I tryed it with using @try and @catch, but it doesn't work. Can I use some alternative handling?

@try{ NSString *test=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@",addressLabel.text,portLabel.text]] encoding:NSUTF8StringEncoding error: nil]; } @catch (NSException *ex) { NSLog(@"Bad IP address"); return; }


Run your XML Parser in NSThread and use notification for errors.


initWithContentsOfURL is a synchronous call. The control will return back from the function only on complete. Try this function is a worker thread so that your main thread will not be blocked.


If you use use NSThread then you have to dive into the memory management unless you are working in XCode 4.2 and using ARC.

So there are two ways for fetching the XML from the server.

1) Use NSURLConnection to get the xml as a NSData object and when you finish loading the data you can simply use that data to initialize an NSString Object. NSURLConnection sends asynchronous call to the server so it will not freeze your view.

2) You can use NSIncovationOperation and NSQueue to fetch your XML and it will also not effect your main thread. like

-(void)myMethod{ NSString *test=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@",addressLabel.text,portLabel.text]] encoding:NSUTF8StringEncoding error: nil]; [self performSelectorOnMainThread:@selector(handleString:) withObject:test]; }

You can use NSInvocationOperation object as follow

NSInvocationOperation *opr = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(myMethod) object:nil]; NSOperationQueue *queue= [NSOperationQueue new]; [queue addOperation:opr];

When the perform selector will be call then you can pass that XML string object to the handleString: method. like

-(void)handleString:(NSString*)xmlString{ // Do something with string }

I hope that it clarifies a little bit of your confusion. All this was to give you an idea how can you achieve your goal without freezing your interface i.e main thread.




You need to launch all long time operations on a second thread to avoid blocking the main thread. Use [self performSelector:@selector(yourXmlDownloadMethod)].


  • Objective-C freezed GUI also with queue
  • JavaFX/SWT WebView synchronous loadcontent()
  • In metro, what's the difference between FolderInformation and StorageFolder
  • Retrieve file size from web server
  • Elixir GenServer parallel handle_call
  • How to call JSON webservice from background in iPhone
  • UILabel Refresh
  • Xcode quits unexpectedly every time I open Xcode
  • Why won't my UITableViewCell deselect and update its text?
  • Trouble downloading file from browser on Android
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Reload UITableView from another tab
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • How to resolve docker host names (/etc/hosts) in containers
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • PushKit for VOIP iOS apps
  • Is it possible to run clang with llc flags
  • Why cepheus don't send int without quotes to orion?
  • Adding elements to a huge XML file
  • How to revert to previous XCode version?
  • How integrated is Collada to OpenGL ES
  • Saving Changes After In-App Purchase Has Been Purchased
  • Assign variable to the value in HTML
  • QLPreviewController hide print button in ios6
  • Jackson Parser: ignore deserializing for type mismatch
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Bad request using file_get_contents for PUT request in PHP
  • Cannot resolve symbol 'MyApi'
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Where to put my custom functions in Wordpress?
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Numpy divide by zero. Why?
  • Android Studio and gradle
  • SQL merge duplicate rows and join values that are different
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why do underscore prefixed variables exist?