If you need a reference to the operation in the block, you should pass it as a parameter to the closure and then you don't need a weak
reference. It will automatically resolve the reference when the closure finishes. For example, consider the following:
let queue = NSOperationQueue()
let concurrentOperation = ABOConcurrentOperation() { operation in
print("\(NSDate()): starting operation")
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * Int64(NSEC_PER_SEC)), dispatch_get_main_queue()) {
print("\(NSDate()): finishing operation")
operation.completeOperation()
}
}
queue.addOperation(concurrentOperation)
And, I defined that closure to be a standard closure:
private var block: ((AsynchronousOperation) -> ())? // FYI, I use optional in case the caller accidentally introduces a strong reference cycle, I can resolve that when the operation completes.
If you have your subclass that prints something in deinit
:
/// a subclass that will just confirm that `deinit` is called
class ABOConcurrentOperation: AsynchronousBlockOperation {
deinit {
print("deinit")
}
}
You'll see that's what happens:
2016-07-07 21:20:54 +0000: starting operation
2016-07-07 21:21:01 +0000: finishing operation
deinit
For your reference, this is the sample AsynchronousOperation
class used above:
/// Asynchronous Operation base class
///
/// This class performs all of the necessary KVN of `isFinished` and
/// `isExecuting` for a concurrent `NSOperation` subclass. So, to developer
/// a concurrent NSOperation subclass, you instead subclass this class which:
///
/// - must override `main()` with the tasks that initiate the asynchronous task;
///
/// - must call `completeOperation()` function when the asynchronous task is done;
///
/// - optionally, periodically check `self.cancelled` status, performing any clean-up
/// necessary and then ensuring that `completeOperation()` is called; or
/// override `cancel` method, calling `super.cancel()` and then cleaning-up
/// and ensuring `completeOperation()` is called.
public class AsynchronousOperation : NSOperation {
override public var asynchronous: Bool { return true }
private let stateLock = NSLock()
private var _executing: Bool = false
override private(set) public var executing: Bool {
get {
return stateLock.withCriticalScope { _executing }
}
set {
willChangeValueForKey("isExecuting")
stateLock.withCriticalScope { _executing = newValue }
didChangeValueForKey("isExecuting")
}
}
private var _finished: Bool = false
override private(set) public var finished: Bool {
get {
return stateLock.withCriticalScope { _finished }
}
set {
willChangeValueForKey("isFinished")
stateLock.withCriticalScope { _finished = newValue }
didChangeValueForKey("isFinished")
}
}
/// Complete the operation
///
/// This will result in the appropriate KVN of isFinished and isExecuting
public func completeOperation() {
if executing {
executing = false
}
if !finished {
finished = true
}
}
override public func start() {
if cancelled {
finished = true
return
}
executing = true
main()
}
override public func main() {
fatalError("subclasses must override `main`")
}
}
/// Asynchronous Operation base class
///
/// This class lets you perform asynchronous block operation. Make sure that the
/// the provided `block` calls `completeOperation`, or else this operation will
/// never finish.
public class AsynchronousBlockOperation : AsynchronousOperation {
private var block: ((AsynchronousOperation) -> ())?
init(block: (AsynchronousOperation) -> ()) {
self.block = block
super.init()
}
override public func main() {
block?(self)
}
override public func completeOperation() {
block = nil
super.completeOperation()
}
}
extension NSLock {
/// Perform closure within lock.
///
/// An extension to `NSLock` to simplify executing critical code.
///
/// - parameter block: The closure to be performed.
func withCriticalScope<T>(@noescape block: Void -> T) -> T {
lock()
let value = block()
unlock()
return value
}
}