Skip to content

jinSasaki/SwiftyBolts

Repository files navigation

SwiftyBolts

Build Status codecov Carthage compatible

A type-safe wrapper for Bolts

Installation

Carthage

github "jinSasaki/SwiftyBolts"

Usage

Handling error in continueWith Method

let task: Task<Bool>
task.continueWith { (task) -> Object? in
    if let error = task.error  {
        // Handling error
        throw error
    }
    let result = task.result
    // Convert to other object
    let object = Object(with: result)
    return object
}

Chaining Tasks

let task: Task<Bool>
task.continueOnSuccessWith { (result) -> Bool? in
    let success = result ?? false
    return success ? "Success!" : "Failure..."
}.continueOnSuccessWith { (result) -> Void in
    let message = result ?? ""
    print(message)
    return
}

Creating Async Methods

func asyncTask() -> Task<String> {
    let tcs = TaskCompletionSource<String>()
    DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) {
        tcs.trySet(result: "ended")
    }
    return tcs.task
}

Tasks in Parallel

Task<String>
    .forCompletionOfAllTasksWithResults([task1, task2])
    .continueWith { (task) -> Void in
        let strings: [String] = task.result ?? []
        print(strings)
        return
}