本文介绍了函数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:

如果对您而言重要的是先执行一项操作,则应改为使用操作依赖项(通过 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上具有不同的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:05