paperlesspost / swift-executor

A lightweight simple implementation of Foundation's Concurrent Operation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

swift-executor

A lightweight simple implementation of Foundation framework Operation

Introduction

Leverages Operation & OperationQueue. AsyncOperation is a concurrent operation that generates the appropriate KVO notifications as required by a concurrent Operation subclass.

Additional Reading:

Apple Concurrency Programming Guide

Apple Operation Class Reference

Supported Platforms:

macOS iOS tvOS watchOS linux

Minimum OS Versions Supported:

macOS 10.10 iOS 9.0 tvOS 9.0 watchOS 2.0 linux

Installation:

Swift Package Manager:

add to your Package.swift:

dependencies: [
   .Package(url: "https://github.com/silverfoxlabs
   /swift-executor", majorVersion: 1, minor: 1)
])

Carthage:

Add to your Cartfile

github "silverfoxlabs/swift-executor"

CocoaPods

in your Podfile:

pod 'swift-executor', :git=> 'https://github.com/silverfoxlabs/swift-executor'

How to use:

The main class to use is AsyncOperation:

class Foo : AsyncOperation {
    
    var bar: String = "Hello World"
    
    override func execute() {
        super.execute()
        
        //Some long running task (or not).
        bar = String(bar.characters.reversed())
        
        //Call finish when done
        finish()
    }
}

then ...

var p = Foo(identifier: "com.your.bundle.id")
p.completionBlock = {
    print("I am a completion block")
}

create an observer for your AsyncOperation ...

struct FooObserver : ExecutorObserver {
    
    func did(start operation: AsyncOperation) {
        dump(self)
        print(#function)
    }
    
    func did(cancel operation: AsyncOperation) {
        dump(self)
        print(#function)
    }
    
    func did(finish operation: AsyncOperation) {
        dump(self)
        print(#function)
    }
    
    func did(becomeReady operation: AsyncOperation) {
        dump(self)
        print(#function)
    }
}
let observer = FooObserver()
p.add(observer: observer)
let first = FooObserver() //observer that is a class
let second = Barz() //observer that is a struct
p.add(observer: observer)
p.add(observer: two)

You can continue to use the normal properties of Operation to manage state: isReady isExecuting isCancelled

you can also use the default properties of Operation

Lastly, to finally execute your operation simply follow the accepted pattern of Operation & OperationQueue

let queue = OperationQueue()
queue.name = "com.someQueue"
queue.addOperation(operation)

About

A lightweight simple implementation of Foundation's Concurrent Operation

License:MIT License


Languages

Language:Swift 67.0%Language:Ruby 33.0%