如果您期望(或渴望)符合这种行为的东西:
t=0 add an operation to the queue. queueucount increments to 1
t=1 add an operation to the queue. queueucount increments to 2
t=2 add an operation to the queue. queueucount increments to 3
t=3 operation completes, queuecount decrements to 2
t=4 operation completes, queuecount decrements to 1
t=5 operation completes, queuecount decrements to 0
<your program gets notified that all operations are completed>
您应该知道,如果将一些“短”操作添加到队列中,您可能会看到这种行为(因为操作是作为添加到队列的一部分开始的):
t=0 add an operation to the queue. queuecount == 1
t=1 operation completes, queuecount decrements to 0
<your program gets notified that all operations are completed>
t=2 add an operation to the queue. queuecount == 1
t=3 operation completes, queuecount decrements to 0
<your program gets notified that all operations are completed>
t=4 add an operation to the queue. queuecount == 1
t=5 operation completes, queuecount decrements to 0
<your program gets notified that all operations are completed>
在我的项目中,我需要知道最后一个操作何时完成,在大量操作被添加到串行 NSOperationQueue(即 maxConcurrentOperationCount=1)之后,并且只有在它们全部完成之后。
谷歌搜索我从一位 Apple 开发人员那里找到了这个声明,以回答“是串行 NSoperationQueue FIFO 吗?”这个问题。 --
如果所有操作具有相同的优先级(在
操作被添加到队列中)并且所有操作总是 -
当它们被放入操作队列时,isReady==YES,然后是串行
NSOperationQueue 是先进先出的。
克里斯·凯恩
可可框架,苹果
在我的情况下,可以知道最后一个操作何时添加到队列中。因此,在添加最后一个操作之后,我向队列中添加了另一个优先级较低的操作,它只发送队列已清空的通知。鉴于 Apple 的声明,这确保了仅在所有操作完成后才发送单个通知。
如果以不允许检测最后一个操作的方式添加操作(即非确定性),那么我认为您必须使用上面提到的 KVO 方法,并添加额外的保护逻辑来尝试检测是否可以添加更多操作。
:)