-1

I used NSXML parser to parse a SOAP response XML i recieved from the web service. In my root method,

 NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

I used this code to send my SOAP request, where theRequest variable has my SOAP request. so after the data is recieved ,

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
     //codes to recieve webData
    xmlParser = [[NSXMLParser alloc] initWithData: _webData];
[_xmlParser setDelegate: self];
[_xmlParser setShouldResolveExternalEntities: YES];
[_xmlParser parse];

 }

Now, the program flows to didStartElement , didFinishDocument methods. My root method needs to return the result obtained after parsing the xml , but when I checked the program flow using breakpoints, the parsing methods doesnot end before the return statement is called in my code and hence I am not able to return the parsed values. How do I solve this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
yabtzey
  • 391
  • 1
  • 7
  • 17

2 Answers2

2

NSXMLParser works completely synchronous. When [_xmlParser parse] returns, all the parsing is done (or aborted with an error).

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Yes when XML parser returns all parsing is done, but by the time all parsing is done, the application flow continues in my program without waiting for xml parser to parse the document. – yabtzey Sep 04 '13 at 08:43
  • @YugeshShrestha: So the problem is that `NSURLConnection` works asynchronously? – Martin R Sep 04 '13 at 08:53
  • Well, yes. Is there anyway I can show a loading bar kind of thing, until the NSURLConnection finishes, that would lock other UI elements but the user will know the application is running and not frozen? – yabtzey Sep 04 '13 at 09:05
  • @YugeshShrestha: You could set `self.view.userInteractionEnabled = NO` to disable user interaction, and display a `UIActivityIndicatorView`. There are also alternative progress indicator views, such as https://github.com/jdg/MBProgressHUD. I haven't used these, so I cannot advise which one is the best. – Martin R Sep 04 '13 at 09:17
  • So I should send a synchoronous request, that will actually halt all the processes until the connection and parsing is completed, and show UIActivityIndicator until the task is completed. Is that what I should do? – yabtzey Sep 04 '13 at 10:19
  • @YugeshShrestha: No, a synchronous request blocks the main thread, so that an activity indicator will probably not work. - You can use `sendAsynchronousRequest` or the delegate-based methods. – Martin R Sep 04 '13 at 10:47
0

According your description, the problem is not quite clear. It seems, the problem is, that - after the root element has been constructed - you continue to process your program while still being within the NSXMLParser's parse method.

To solve this, just invoke a selector on the main thread passing the parser's delegate result (root element) to a method which eventually handles it. Alternatively, use dispatch_async to the main thread, where you invoke a method which handles the root element.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67