9

Today I've met one weird issue when I was trying to 'generalize' my 'CoreData importing operations'. It appeared that if I create a generic subclass of NSOperation the main() func won't be called.

Simple example:

class MyOperation<T: NSObject>: NSOperation {

    override func main() {
        println("My operation main was called")
    }
}

If you create an instance of this class and add it to the operationQueue you will see that it's main() isn't actually called.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.operationQueue = NSOperationQueue()
    let operation = MyOperation<NSString>()
    self.operationQueue!.addOperation(operation)
}

Operation simply transits from ready to executing and to finished state without calling main().

If I remove generic annotation <T: NSObject> from MyOperation class it will work fine.

How is this possible? Am I missing something here?

Grimxn
  • 22,115
  • 10
  • 72
  • 85
Nevs12
  • 599
  • 6
  • 13

3 Answers3

12

Workaround: You can create NSOperation subclass (no generic), override main and call you own 'execute' func, which can be overriden by generic subclasses. Example:

class SwiftOperation : NSOperation {

    final override func main() {
        execute()
    }

    func execute() {
    }

}

class MyOperation<T> : SwiftOperation {

    override func execute() {
        println("My operation main was called")
    }

}
Sergey Topal
  • 171
  • 2
  • 7
7

The problem is caused by this simple rule:

Method in a generic class cannot be represented in Objective-C

As a result, when bridged to Objective-C, MyOperation looks like pure, with no methods are overridden, NSOperation subclass.

You can see this error by marking override func main() with @objc attribute.

@objc override func main() {  // < [!] Method in a generic class cannot be represented in Objective-C
    println("My operation main was called")
}
rintaro
  • 51,423
  • 14
  • 131
  • 139
  • 2
    Thank you for clarification. It is sad that we cannot use so powerful technique in Swift with Objective-C. In this particular case, 'generalized' operations could help to reuse quite a few bunches of code. Well, yeah. :/ – Nevs12 Sep 30 '14 at 06:32
1

In Xcode 7 generic NSOperation has been fixed: if I run this code in a playground it works:

protocol SomeProtocol {

    // markup protocol
}

class GenericOperation<SomeTypeImplementingProtocol: SomeProtocol>: NSOperation {

    let referenceToSomeTypeImplementingProtocol: SomeTypeImplementingProtocol

    init(referenceToSomeTypeImplementingProtocol: SomeTypeImplementingProtocol) {

        self.referenceToSomeTypeImplementingProtocol = referenceToSomeTypeImplementingProtocol
    }

    override func main() {

        debugPrint("The GenericOperation main() method was called.")

    }
}

class TypeImplementingSomeProtocol: SomeProtocol {


    init() {

    }
}


let operationQueue = NSOperationQueue()

let typeImplementingSomeProtocolInstance = TypeImplementingSomeProtocol()


let operation = GenericOperation<TypeImplementingSomeProtocol>(referenceToSomeTypeImplementingProtocol: typeImplementingSomeProtocolInstance)


operationQueue.addOperation(operation)
sebastienhamel
  • 313
  • 2
  • 10