When does the NSOperationQueue remove an operation from the queue?
Asked Answered
L

2

16

I need to know that When does a NSOperationQueue remove an operation from the queue? I have NSOperationQueue which has list of NSOperation. At which point the NSOperationQueue removes an operation from queue?

  1. After starting an operation? OR
  2. After Finish/Cancel an operation?

Because I need a notification when all the operations in NSOPerationqueue are finished. For this I referred this link

Ludmilla answered 3/2, 2014 at 6:49 Comment(2)
after finish or cancelThermodynamic
In operation complete block or fail check count of operation in operation queue (send notification or use delegate)Thermodynamic
C
14

According to Apple Developer Reference

An operation queue executes its queued NSOperation objects based on their priority and readiness. After being added to an operation queue, an operation remains in its queue until it reports that it is finished with its task.

So, NSOperationQueue removes an operation after it has been finished.

Source - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html

Calumet answered 3/2, 2014 at 6:53 Comment(2)
Note that if an operation is cancelled, it merely sets the appropriate flags within the operation to mark it as such - The operation itself is responsible for checking if it is cancelled and marking itself finished accordingly. An operation that has not started should check if it is cancelled when it starts and return immediately after marking itself finished. An operation which is executing would need to periodically check if it is cancelled and handle that in a similar manner. See developer.apple.com/reference/foundation/nsoperation/…Wilderness
@Wilderness remark should be it's own independent answer. This was very important and helpful to note that cancelation does not mean immediate removal from the queue.Sentience
W
6

NSOperationQueue manages its internal queue of NSOperations. An NSOperation will only be removed from the queue after it is canceled or finished.

Note that if an NSOperation is cancelled, it merely sets the appropriate flags within the NSOperation object to mark it as such - The implementation of the NSOperation is responsible for checking if it is cancelled and marking itself finished accordingly. An operation that has not started should check if it is cancelled when it starts and return immediately after marking itself finished. A long-running operation which is executing would need to periodically check if it is cancelled and handle that state in a similar manner.

It also makes sense to do this check before executing destructive code or code that will mutate data, such as saving or deleting a managed object from core data.

See https://developer.apple.com/reference/foundation/nsoperation/1411672-cancel?language=objc

Wilderness answered 18/10, 2017 at 21:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.