函数setQueuePriority在iOS模拟器和iPhone上具有不同的性能 [英] function setQueuePriority has different performance on iOS emulator and on iPhone

查看:28
本文介绍了函数setQueuePriority在iOS模拟器和iPhone上具有不同的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个NSOperationQueue中有两个NSIvocationOperation.然后,我使用setQueuePriority来调整两个操作(1-> 2或2-> 1)的执行顺序.该代码在模拟器上运行良好,但是在我的iPhone上失败了.

I have two NSIvocationOperations in one NSOperationQueue. And I used setQueuePriority to adjust the execution order of the two operations, 1->2, or 2->1. The code worked well on emulator, however, failed on my iPhone.

我的代码是这样的:

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
operationQueue.maxConcurrentOperationCount = 1;

NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1) object:nil];
[operation1 setQueuePriority:NSOperationQueuePriorityVeryLow];

NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread2) object:nil];
[operation2 setQueuePriority:NSOperationQueuePriorityHigh];

[operationQueue addOperation:operation1];
[operationQueue addOperation:operation2];

Operation2应该在operation1之前执行,我在模拟器上看到了相同的结果.在iPhone上,无论我如何调整其QueuePriorities,始终首先执行operation1.为什么会这样?

Operation2 should be executed ahead of operation1 and I saw the same result on emulator. While on iPhone, operation1 was always executed first no matter how I adjusted their QueuePriorities. Why it's like this?

推荐答案

队列优先级不能保证执行顺序.引用苹果的文档:

Queue priority doesn't guarantee execution order. To quote from Apple's documentation:

您仅应在需要时使用优先级值对相对值进行分类非依赖操作的优先级.优先级值不应为用于实现不同操作之间的依赖关系管理对象.

You should use priority values only as needed to classify the relative priority of non-dependent operations. Priority values should not be used to implement dependency management among different operation objects.

如果对您而言重要的是先执行一项操作,则应改为使用操作依赖项(通过 addDependency:).

If it's important to you that one operation be executed before another you should use operation dependencies instead (via addDependency:).

这篇关于函数setQueuePriority在iOS模拟器和iPhone上具有不同的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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