gpt4 book ai didi

ios - set MaxConcurrentOperationCount =1 有什么好处

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:44:59 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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 addOperation:operation];
}

因此您在循环中添加新操作,但它们将在串行队列中运行。尽管如此,来自 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 - set MaxConcurrentOperationCount =1 有什么好处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19636601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com