OStack程序员社区-中国程序员成长平台

标题: ios - 设置 MaxConcurrentOperationCount =1 有什么好处 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:08
标题: ios - 设置 MaxConcurrentOperationCount =1 有什么好处

我正在阅读 NSOperationQueue 博客和文档。

我们可以在NSOperationQueue中通过setMaxConcurrentOperationCount属性设置并发操作数。

我的疑问是如果我使用 setMaxConcurrentOperationCount =1 那么,这意味着我的队列一次只能执行一个任务。那么并发是什么意思呢,变成了单线程执行。

请告诉我,如果我解释错误的意思。



Best Answer-推荐答案


是的,它一次只会执行一项任务。在某些情况下,一次设置一个是有意义的。例如,下载照片并对其应用过滤器。从您的角度来看,最好一次下载一个,应用过滤器并存储/显示它。正如乔纳森在评论中添加的那样:

Instead of setting maxConcurrentOperationCount=1. You could make an operation dependent on the completion of another operation which would essentially have the same effect. E.g. A depends on B, but make sure B doesn't depend on A.

setMaxConcurrentOperationCount =1 非常适合这种东西:

[myOperationQueue setMaxConcurrentOperationCount:1];
for (NSOperation *operation in myArrayOfOperations )
{
   [myOperationQueue addOperationperation];
}

因此,您在循环中添加新操作,但它们将在串行队列中运行。不过,来自 documentation :

An operation queue executes its queued operation objects based on their priority and readiness. If all of the queued operation objects have the same priority and are ready to execute when they are put in the queue—that is, their isReady method returns YES—they are executed in the order in which they were submitted to the queue. For a queue whose maximum number of concurrent operations is set to 1, this equates to a serial queue. However, you should never rely on the serial execution of operation objects. Changes in the readiness of an operation can change the resulting execution order.

关于ios - 设置 MaxConcurrentOperationCount =1 有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19636601/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4