Last Updated: February 25, 2016
·
784
· kkoval

Async code in Swift

Async code was never so easy

var image: UIImage!
    Async.background {
      // Do long running tasks here
      let url = NSURL(string: "http://lorempixel.com/400/400/")
      image = UIImage(data: NSData(contentsOfURL: url))
    }.main {
      // update UI
      println("back to main thread")
      self.view.addSubview(UIImageView(image: image))
    }

    let longWork = Async.background {
      //work
    }
    //Other work. descided to cancel
    longWork.cancel()

    //Start with delay
    Async.main(after: 2) {
      //Move to next screen
    }
  • chain block
  • set delay
  • blocks are cancelable

Get lib here.
https://github.com/duemunk/Async