NSXMLParser의 구문 분석 메서드가 비동기입니까?NSXMLParser의 비동기 구문 분석 메서드
다른 말로하면 NSXMLParse 객체가 있고 기본 스레드에서 [someParseObject parse]
을 호출하면 주 스레드를 차단하지만 해당 작업을 수행합니까?
NSXMLParser의 구문 분석 메서드가 비동기입니까?NSXMLParser의 비동기 구문 분석 메서드
다른 말로하면 NSXMLParse 객체가 있고 기본 스레드에서 [someParseObject parse]
을 호출하면 주 스레드를 차단하지만 해당 작업을 수행합니까?
비동기 적이 아니므로 기본 스레드를 차단합니다.
NSXMLParser는 URL/데이터를 구문 분석 할 수 있습니다. URL을 직접 구문 분석하면 NSXMLParser를 사용하여 데이터 구문 분석을 사용할 수있는 대신 UI (기본 스레드)가 고정됩니다. NSURLConnection API를 통해 데이터를 비동기 적으로 가져옵니다.
예. 차단됩니다.
-(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];
}
는이 NSXMLParser 같이 할 수 있습니다 : 여기에 내가 구문 분석 할 때 ... UI 스레드를 차단 단지 문자열로 URL 경로 beginParsing를 호출하지 NSInvocationQueue을 사용하고이 알아서하는 방법입니다 비동기식으로
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString * [email protected]"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];
});
});
[xmlParser parse]는 주 스레드에서 실행되고 차단됩니다. 비동기 메소드 만 초기화하십시오. –
@PhilipKramarov는 말했습니다. – nmdias
왜 경로 매개 변수를 해제 하시겠습니까? – jcpennypincher