Asynchronous programming can be tricky, especially for less experienced developers. There are many things to consider like thread the code is executing on, priorities or deadlocks. There are also many tools and practices that make it easier to write asynchronous code.

In this post I would like to compare different styles of writing asynchronous code in Swift. Most of it also apply to Objective-C and other languages (like Java or C#) because concepts between platforms are shared very often. Main difference would be syntax and libraries used.

If you want to skip the basics jump to Promises section.

Let’s assume we are in common scenario where we have to download some data from a service and process it. Seems like an easy task, right? One could just write it like this:

let downloadedData = try! Data(contentsOf: url)
let image = UIImage(data: downloadedData)!
let processedImage = process(image)
updateUI(processedImage)
// voilá!

About

Piotr Tobolski

iOS developer @ intive | https://piotrtobolski.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store