Latest 0.1.0
Homepage https://github.com/OliverLetterer/swift-async
License MIT
Platforms ios 8.0, requires ARC
Frameworks Foundation
Authors

swift-async provides a common interface for asynchronous control flow. It allows You to group async tasks, like loading data from the network or performing long running computations, together with a unified completion handler. Inpired by node-async.

Usage

For the sake of simplicity, consider the following model:

class Room {
    let number: Int
}

class Floor {
    let name: String
}

class Building {
    func performAsyncWork1(completionHandler: (NSError?) -> ()) {
        // perform async work and invoke completionHandler when done
    }

    func fetchRoomsOnFloor(floor: Floor, completionHandler: ([Room]?, NSError?) -> ()) {
        // fetch some data here and invoke completionHandler when done
    }

    func fetchFloors(completionHandler: ([Floor]?, NSError?) -> ()) {
        // fetch some data here and invoke completionHandler when done
    }
}

Executing tasks in parallel

let building1 = Building()
let building2 = Building()

let tasks = [
    Async.bind { building1.fetchFloors($0) },
    Async.bind { building1.fetchRoomsOnFloor(Floor(name: "First floor"), $0) },

    Async.bind { building2.fetchFloors($0) },
    Async.bind { building2.fetchRoomsOnFloor(Floor(name: "First floor"), $0) },
]

Async.parallel(tasks) { (results, error) in
    if let error = error {
        println("Error: (error)")
    } else if let results = results {
        let floors = results[0] as [Floor]
        let rooms = results[1] as [Room]

        println("Got floors: (floors)")
        println("And rooms: (rooms)")
    }
}

Executing tasks one after another

let building1 = Building()
let building2 = Building()

let tasks = [
    building1.performAsyncWork1,
    building2.performAsyncWork2,
    Async.bind { building1.performAsyncWorkWithObject(nil, completionHandler: $0) },
]

Async.series(tasks) { (error) in
    if let error = error {
        return println("Error: (error)")
    } else {
        println("all tasks succeeded")
    }
}

Asumptions

All functions must take a unified completion handler that either takes

  • a single NSError? argument
  • or a generic type T? and a NSError? argument

swift-async is not thread safe, so You need to make sure that all completion handlers are executed on the same thread.


Contact

License

swift-async is released under an MIT license. See LICENSE for more information.

Latest podspec

{
    "name": "swift-async",
    "version": "0.1.0",
    "summary": "Common interface for asynchronous control flow in #swift.",
    "homepage": "https://github.com/OliverLetterer/swift-async",
    "license": "MIT",
    "authors": {
        "Oliver Letterer": "[email protected]"
    },
    "source": {
        "git": "https://github.com/OliverLetterer/swift-async.git",
        "tag": "0.1.0"
    },
    "social_media_url": "https://twitter.com/oletterer",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "async/*.h",
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This