Search code examples
nsoperationfoundation

Why apple says Operation is abstract class?


If swift does not have abstract class, then why apple says Operation is abstract class?

From the foundation documentation, operation is:

An abstract class that represents the code and data associated with a single task.


Solution

  • Also from the same documentation:

    Because the NSOperation class is an abstract class, you do not use it directly but instead subclass or use one of the system-defined subclasses (NSInvocationOperation or NSBlockOperation) to perform the actual task. Despite being abstract, the base implementation of NSOperation does include significant logic to coordinate the safe execution of your task. The presence of this built-in logic allows you to focus on the actual implementation of your task, rather than on the glue code needed to ensure it works correctly with other system objects.

    NSOperation has its concrete counterparts, for example, NSBlockOperation

    The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.

    Blocks added to a block operation are dispatched with default priority to an appropriate work queue. The blocks themselves should not make any assumptions about the configuration of their execution environment.

    Although Objective-C does not provide abstract classes formally as other programming languages do (E.G: Java) it's possible to implement them, and Apple does it in many elements of their SDKs.