主线程上的XML解析速度更快吗?为什么? [英] Is parsing XML on the main thread faster? Why?

查看:93
本文介绍了主线程上的XML解析速度更快吗?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LRResty和NSXMLRequest来显示来自API的搜索结果.这是我的代码:

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?

推荐答案

主线程的性能会降低,因为应用程序还有很多工作要做.创建线程本身会产生一些开销,然后必须在忙于处理UI事件和绘制UIView的主线程与进行解析的后台线程之间共享处理时间.如果您在主线程上进行解析,则将阻止应用程序对UI进行任何操作,因此它要做的事更少,并且可以更快地完成.但是您没有UI更新,因此显然这不好.

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.

这篇关于主线程上的XML解析速度更快吗?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆