ios - 设置 MaxConcurrentOperationCount =1 有什么好处
<p><p>我正在阅读 <code>NSOperationQueue</code> 博客和文档。</p>
<p>我们可以在<code>NSOperationQueue</code>中通过<code>setMaxConcurrentOperationCount</code>属性设置并发操作数。</p>
<p>我的疑问是如果我使用 <code>setMaxConcurrentOperationCount =1</code> 那么,这意味着我的队列一次只能执行一个任务。那么并发是什么意思呢,变成了单线程执行。</p>
<p>请告诉我,如果我解释错误的意思。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>是的,它一次只会执行一项任务。在某些情况下,一次设置一个是有意义的。例如,下载照片并对其应用过滤器。从您的角度来看,最好一次下载一个,应用过滤器并存储/显示它。正如乔纳森在评论中添加的那样:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p><code>setMaxConcurrentOperationCount =1</code> 非常适合这种东西:</p>
<pre><code>;
for (NSOperation *operation in myArrayOfOperations )
{
;
}
</code></pre>
<p>因此,您在循环中添加新操作,但它们将在串行队列中运行。不过,来自 <a href="https://developer.apple.com/library/mac/documentation/cocoa/reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperationQueue/addOperation%3a" rel="noreferrer noopener nofollow">documentation</a> :</p>
<blockquote>
<p>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.</p>
</blockquote></p>
<p style="font-size: 20px;">关于ios - 设置 MaxConcurrentOperationCount =1 有什么好处,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19636601/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19636601/
</a>
</p>
页:
[1]