Git Product home page Git Product logo

await's Introduction

Await

Swift port of C# Await using Cocoa's Run Loop mechanism.

(Useful for unit testing, but needs special care for application use)

Without Await

showProgressHUD("Start!")     // displays AFTER download finished (you know why)
let image = downloadLargeImageFromWeb()  // OMG downloading from main-thread!!!
showProgressHUD("Finished!")  // displays after download finished
showProgressHUD("Start!")  // main-thread

// OK let's use dispatch_async then...
dispatch_async(globalQueue) {
    image = downloadLargeImageFromWeb()  // background-thread
    
    dispatch_async(dispatch_get_main_queue()) {
        showProgressHUD("Finished!")  // main-thread

        // want more nested blocks here?
    }
}

With Await

showProgressHUD("Start!")

let image = await { downloadLargeImageFromWeb() }

// because of await's running Run Loop, "Start!" will be displayed immediately here

showProgressHUD("Finished!")  // displays after download finished

How to use

await

let image = await { downloadLargeImageFromWeb() }

await calls dispatch_async for given closure & runs Run Loop until finished.

await + until

var shouldStop = false
var container: SomeContainer

let result = await(until: shouldStop) {
    container.data = nil

    dispatch_async(globalQueue) {
        container.data = downloadData()
        shouldStop = true
    }

    return container.data
}

Use until to manually adjust the Run Loop running time. This is especially useful in combination with async-programming monads like Promise.

Take a look at PromiseKit example:

import PromiseKit
var promise = Promise<NSData> { (fulfiller, rejecter) in ... }
...

let result = await(until: !promise.pending) { promise.value }
// or, let result = await(promise)

await + timeout

let image = await(timeout: 3) { downloadLargeImageFromWeb() }  // returns nil if 3 sec has passed

await + finishable closure

You can even use finish() or finish(returnValue) inside closure to manually stop running Run Loop instead of using await + until.

let data = await { finish in
    dispatch_async(queue) {
        let d = downloadData()
        finish(d)  // pass result data
    }
}
var data: NSData?
await { finish in
    dispatch_async(queue) {
        let d = downloadData()
        data = d
        finish()  // no return
    }
}

For application use

For applicaiton use, you MUST wrap await calls with async as follows:

async {
    var image = await { downloadLargeImageFromWeb() }
    image = await { filterImage(image) }
}

This is to ensure that await will not block dispatch_get_main_queue() which often causes UIKit not responding to touches.

Limitation

await + timeout and nested awaits may not finish at proper time inside async due to nested RunLoop running.

await's People

Contributors

inamiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

uikit0 mallexxx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.