NSOperationQueue似乎会降低性能 [英] NSOperationQueue seems to slow performance

查看:141
本文介绍了NSOperationQueue似乎会降低性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是NSOperationQueue的新手,但是在当前项目中,我需要一种方法来在异步操作超时时取消它.我的原始代码使用GCD并可以正常工作.在这里:

I am new to NSOperationQueue, but in a current project I need a way to cancel an asynchronous operation when it times out. My original code uses GCD and works. Here it is:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^{
    rg = [[RiverGauge alloc] initWithStationInfo:[station id] forHistoryInHours:48 inThisFormat:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud removeFromSuperview];
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        GaugeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GaugeDetailViewController"];
        [vc setRiverGauge:rg];
        [self.navigationController pushViewController:vc animated:YES];
    });
});

RiverGauge是一项网络操作,可能需要很长时间,特别是如果用户在农村地区.我见过的大多数帖子都建议将其移到NSOperationQueue中.我这样做如下:

RiverGauge is a network operation that could take a long time, especially if the user is in rural areas. Most posts I've seen suggest moving this into an NSOperationQueue. I have done so as follows:

NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getGaugeDetail:) object:[station id]];
[queue addOperation:op];

在getGaugeDetail方法中(在选择器参数中),我有以下代码:

In the getGaugeDetail method (in the selector parameter) I have the following code:

RiverGauge *rg = [[RiverGauge alloc] initWithStationInfo:gaugeIdentifier forHistoryInHours:48 inThisFormat:nil];
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
GaugeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"GaugeDetailViewController"];
[vc setRiverGauge:rg];
[self.navigationController pushViewController:vc animated:YES];

使用GCD进行此操作时,它会以快速的Internet连接快速响应.但是,使用NSOperationQueue时,相同的操作将花费几秒钟.但是,返回的数据是准确的.同样,我对NSOperationQueue不太熟悉.有什么建议吗?

When doing this operation using GCD, it responds quickly with a fast internet connection. However, when using NSOperationQueue, the same operation takes several seconds. The returned data is accurate, however. Again, I'm not very familiar with NSOperationQueue. Any suggestions?

谢谢!

推荐答案

在代码中,使用NSOperationQueue时,您似乎在NSOperationQueue中对UI进行了更改,这大概是在除线程之外的其他线程上执行的主线程.有时,当您在主线程之外与UIKit元素进行交互时,它们要么无法正常工作,要么在出现结果之前会有明显的延迟.所以我的猜测是,您正在看到在错误的线程上进行UI更改的效果.

In your code when using an NSOperationQueue, it looks like you're making your UI changes in the NSOperationQueue, which would presumably be performing these on a thread other than the main thread. Sometimes when you interact with UIKit elements off the main thread, they either don't work correctly or there's a noticeable delay before the results of them appear. So my guess is that you're seeing the effects of making UI changes on the wrong thread.

尝试在主线程上重新执行UIKit,并查看它是否可以解决您的问题.您可能可以继续使用GCD来做到这一点.

Try performing the UIKit stuff back on the main thread and see if it seems to fix your issue. You can probably continue to use GCD to do that.

这篇关于NSOperationQueue似乎会降低性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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