Is NSXMLParser's parse method asynchronous

前端 未结 4 524
清酒与你
清酒与你 2021-02-14 08:29

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

4条回答
  •  迷失自我
    2021-02-14 09:07

    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];
    }
    

提交回复
热议问题