问题
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