Is parsing XML on the main thread faster? Why?

情到浓时终转凉″ 提交于 2019-12-25 01:27:06

问题


I'm using LRResty and NSXMLRequest to display search results from an API. Here's what my code looks like:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[[LRResty client] get:searchEndpointURL withBlock:^(LRRestyResponse *response) {

    //NSLog(@"Results:\n\n\n%@", [response asString]);

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[response responseData]];
    parser.delegate = self;

    //TODO: Why does this work faster than running in the background?
    [parser parse];

    //[parser performSelectorInBackground:@selector(parse) withObject:nil]
}];

For some reason, my Search Display Controller updates more quickly when I parse blocking the main thread than without.

Does the slower performance have to do with the fact that the parser is not running on the main thread? How so?


回答1:


The performance will be slower off the main thread because there is more for the application to get done. Creating the thread itself incurs some overhead, and then the processing time has to be shared between the main thread, which is busy handling UI events and drawing UIViews, and the background thread that is doing the parsing. If you are parsing on the main thread you are preventing the app doing anything with the UI, so there is less for it to do and it will complete sooner. But you get no UI updates, so obviously this is no good.



来源:https://stackoverflow.com/questions/9843475/is-parsing-xml-on-the-main-thread-faster-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!