Is NSXMLParser's parse method asynchronous
Asked Answered
I

4

10

Is NSXMLParser's parse method asynchronous?

in other words if i have an NSXMLParse object and I call [someParseObject parse] from the main thread, will it block the main thread while it does its thing?

Icon answered 19/5, 2010 at 4:43 Comment(0)
W
18

It is not asynchronous so it will block the main thread.

Wrench answered 19/5, 2010 at 4:48 Comment(0)
C
1

NSXMLParser can parse URL/Data, If we parse URL directly, it will freeze the UI(Main Thread),instead of that you can use Data Parsing by using NSXMLParser.Please go through NSURLConnection API for asynchronous fetching the data.

Chili answered 12/10, 2010 at 4:52 Comment(0)
M
0

Yes it blocks. Here is how i have used NSInvocationQueue to not block the UI thread when parsing... just call beginParsing with the url path as a string and it will take care of the rest:

-(void) beginParsing:(NSString*) path{
    if(path ==nil)
        return;

    NSOperationQueue *queue = [[NSOperationQueue new] autorelease];

    NSInvocationOperation *operation= [[[NSInvocationOperation alloc]
                                             initWithTarget: self
                                                   selector: @selector(createRequestToGetData:)
                                                     object: path]
                                       autorelease];

    [queue addOperation:operation];
}

-(void)createRequestToGetData:(NSString*)path
{
    NSURL* Url = [NSURL URLWithString:path];

    NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:Url];

    [parser setDelegate:self];

    NSLog(@"path is %@",path);
    [parser parse];

    [path release];
    [parser release];
}
Machicolate answered 30/11, 2012 at 20:51 Comment(1)
Why are you releasing the path parameter?Tympanitis
K
0

you can do like this NSXMLParser as asynchronous

dispatch_async( dispatch_get_global_queue(0, 0), ^{

    NSString * dovizUrl=@"http://www.tcmb.gov.tr/kurlar/today.xml";
    NSURL *url = [NSURL URLWithString:dovizUrl];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    xmlParser.delegate = self;
    // call the result handler block on the main queue (i.e. main thread)
    dispatch_async( dispatch_get_main_queue(), ^{
        // running synchronously on the main thread now -- call the handler
        [xmlParser parse];
    });
});
Kristopher answered 14/12, 2013 at 19:44 Comment(2)
[xmlParser parse] runs on the main thread and will block it. You only init the method asynchronous.Biparous
What @PhilipKramarov said.Gersham

© 2022 - 2024 — McMap. All rights reserved.