OperationKit 2.1.2

OperationKit 2.1.2

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Feb 2017
SwiftSwift Version 3.0-GM-CANDIDATE
SPMSupports SPM

Maintained by Jorge Orjuela.



  • By
  • Jorge Mario Orjuela Gutierrez

A Swift framework inspired by WWDC 2015 Advanced NSOperations session.

Requirements

  • iOS 8.0+
  • Xcode 8.1+
  • Swift 3.0+

Usage

Operation is an Foundation.Operation subclass. It is an abstract class which should be subclassed.

import OperationKit

class FooOperation: Operation {
    override func execute() {
        print("running")
        finish()
    }
}

let operationQueue = OperationQueue()
let fooOperation = FooOperation()
operationQueue.addOperation(fooOperation)

Observers

Observers are attached to an Operation. They receive callbacks when operation events occur.:

operation.addObserver(BlockObserver { operation, _ in
    print("finished")                     
})

OperationKit also provides TimeoutObserver and NetworkObserver.

Conditions

Conditions are attached to an Operation. Before an operation is ready to execute it will asynchronously evaluate all of its conditions. If any condition fails, the operation finishes with an error instead of executing. For example:

let urlRequestOperation = URLRequestOperation(request: request)
urlRequestOperation.addCondition(ReachabilityCondition(host: request.url!))

Making a Request

import OperationKit

let operationQueue = OperationKit.OperationQueue()
let dataRequestOperation = DataRequestOperation(request: request)
operationQueue.addOperation(operationQueue)

Response Handling

Handling the Response of a DataRequest made in OpertationKit is easy.

dataRequestOperation.responseJSON { result in
    switch result {
          case let .success(responseJSON):
                print(responseJSON)

           case let .failure(_error):
               print("error")
    }
}