1

In my "didFinishLaunchingWithOptions" method, I have created a UIProgressView in my GUI & after that I call a method to call a WebService with a NSURLConnection to get an XML with a SOAP message.

In the delegate method "connectionDidFinishLoading" I parse the XML with NSXMLParser in another class.

The problem is that I want to update my UIProgressView while the XML is parsed, but it is updated after the whole XML has been parsed. I have heard this is because the NSURLconnection run on the main thread and is blocking the UI.

How can I parse and update the progress bar in the same time ?

- (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];
}
Lelka
  • 41
  • 6

2 Answers2

1

Hey you can update the progress bar in
-(void)connection:(NSURLConnection *)connection didReceiveData: (NSData *)data { //update progress bar here
}

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • Thank you for your reply ! But I don't want to update the progress bar while receiving the XML but while parsing it in "connectionDidFinishLoading". Do you understand ? – Lelka Nov 02 '11 at 09:09
  • ok..parsing.... for that you have to update the progressbar at various points... share your code of did recived response.... – Inder Kumar Rathore Nov 02 '11 at 09:20
  • - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength:0]; } (SOrry, I don't know how to wrap my code for better reading...) – Lelka Nov 02 '11 at 09:33
  • sorry sorry... I mean connectionDidFinishLoading method..and for readable code please edit your question for that and add code to it – Inder Kumar Rathore Nov 02 '11 at 09:37
  • In the "didEndElement" delegate method of my XMLParser class. Each time I encounter a special tag, I do : "pgrView.progress = /*my updated float*/" – Lelka Nov 02 '11 at 10:18
  • The "UIProgressView" is in the same class as the "NSURLConnection". And the parsing is made in another class "XMLParser" – Lelka Nov 02 '11 at 10:20
  • That cool that you are updating you progress bar when ever you encounter a new tag... But as I think your parsing takes very less time might be less than 0.2 seconds and your progress bar has only 0.2 sec to show your progress and I think that is very less time and it might be possible that it's prgress is not visible. for testing you can add sleep method of NSThread to sleep it for some (sy 100) miliseconds then see if it is progressing or not. Can i know how big is your xml? – Inder Kumar Rathore Nov 02 '11 at 10:50
  • The size of my XML is 2 MB. There is about 40000 lines... Where do I pause the Thread ? – Lelka Nov 02 '11 at 11:18
  • just before the `pgrView.progress = /*my updated float*/` this code add `[NSThread sleepForTimeInterval:100];' – Inder Kumar Rathore Nov 02 '11 at 11:21
  • It doesn't work. And I think it's "[NSThread sleepForTimeInterval:0.01];" for 100 milliseconds – Lelka Nov 02 '11 at 11:45
  • My UIProgressView is not refreshed – Lelka Nov 02 '11 at 11:47
  • 0.01 means 0.01 ms and 100 means 100ms so change it to 100 – Inder Kumar Rathore Nov 02 '11 at 11:51
  • 100 is for 100 seconds. I'm sure of it because I tested it with a NSLog. Every 100 seconds, the NSLog is executed... And it still doesn't update my progress bar... – Lelka Nov 02 '11 at 12:30
0

NSURLConnection does not block the main thread when you are loading data, but all the stuff you did in connectionDidFinishLoading: will. If you know how many elements might be in the document, you can use the NSXMLParserDelegate callback: - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict to keep a running total of seen elements, and the "percent complete" will be that number divided by the total number of elements. In that callback you can then update your progress bar.

Without knowing a priori the length of the document it'll be hard to estimate how far along it is in processing, unless you keep a running total of the number of bytes processed instead.

Mike Katz
  • 2,060
  • 2
  • 17
  • 25