//
//  ComposedOperation.swift
//  Operations
//
//  Created by Daniel Thorpe on 28/08/2015.
//  Copyright © 2015 Daniel Thorpe. All rights reserved.
//

import Foundation

public class ComposedOperation<T: NSOperation>: Operation {

    public let target: Operation
    public var operation: T

    public convenience init(_ op: T) {
        self.init(operation: op)
    }

    init(operation op: T) {
        self.target = op as? Operation ?? GroupOperation(operations: [op])
        self.operation = op
        super.init()
        name = "Composed Operation"
        target.name = target.name ?? "Composed <\(T.self)>"
        target.addObserver(DidFinishObserver {
            [unowned self] _, errors in
            self.finish(errors)
            })
    }

    public override func cancel() {
        operation.cancel()
        super.cancel()
    }

    public override func execute() {
        target.log.severity = log.severity
        produceOperation(target)
    }
}

//
//  ComposedOperation.swift
//  Operations
//
//  Created by Daniel Thorpe on 28/08/2015.
//  Copyright © 2015 Daniel Thorpe. All rights reserved.
//

import Foundation

public class ComposedOperation<T: NSOperation>: Operation, OperationDidFinishObserver {

    public let target: Operation
    public var operation: T

    public convenience init(_ operation: T) {
        self.init(operation: operation)
    }

    init(operation composed: T) {
        target = composed as? Operation ?? GroupOperation(operations: [composed])
        operation = composed
        super.init()
        name = "Composed Operation"
        target.name = "Composed <\(T.self)>"
        target.addObserver(self)
    }

    public override func cancel() {
        target.cancel()
        operation.cancel()
        super.cancel()
    }

    public override func execute() {
        target.log.severity = log.severity
        produceOperation(target)
    }

    public func didFinishOperation(operation: Operation, errors: [ErrorType]) {
        if operation === target {
            finish(errors)
        }
    }
}

--- 
+++ 
@@ -8,26 +8,28 @@
 
 import Foundation
 
-public class ComposedOperation<T: NSOperation>: Operation, OperationDidFinishObserver {
+public class ComposedOperation<T: NSOperation>: Operation {
 
     public let target: Operation
     public var operation: T
 
-    public convenience init(_ operation: T) {
-        self.init(operation: operation)
+    public convenience init(_ op: T) {
+        self.init(operation: op)
     }
 
-    init(operation composed: T) {
-        target = composed as? Operation ?? GroupOperation(operations: [composed])
-        operation = composed
+    init(operation op: T) {
+        self.target = op as? Operation ?? GroupOperation(operations: [op])
+        self.operation = op
         super.init()
         name = "Composed Operation"
-        target.name = "Composed <\(T.self)>"
-        target.addObserver(self)
+        target.name = target.name ?? "Composed <\(T.self)>"
+        target.addObserver(DidFinishObserver {
+            [unowned self] _, errors in
+            self.finish(errors)
+            })
     }
 
     public override func cancel() {
-        target.cancel()
         operation.cancel()
         super.cancel()
     }
@@ -36,10 +38,4 @@
         target.log.severity = log.severity
         produceOperation(target)
     }
-
-    public func didFinishOperation(operation: Operation, errors: [ErrorType]) {
-        if operation === target {
-            finish(errors)
-        }
-    }
 }
Fix retain cycle in composed operation.
