Asynchronous programming can be a real pain in the lemon. Unless you’re extremely careful, it can easily result in humongous delegates, messy completion handlers and long nights debugging code! Lucky for you, there’s a better way: promises. Promises tame asynchronicity by letting you write code as a series of actions based on events. This works […]
A (good) while ago I made my own bare bone and easy-to-use Promise class for Swift: Swift Promises (Github)
Updated every so slightly here and there since the Swift 2 days to accommodate for the language’s changes.
Fair warning, it is really bare bones. But I’ve been using it for practically all of the iOS apps I’ve worked on in the past 12+ months and has served me really well for async related tasks.
It was interesting how developers were already creating their own Promise libraries for Swift since the beta days. Both PromiseKit and BrightFutures are the names I’ve seen pop-up most over the years. And I’m glad your article has gone in-depth with one of them. It certainly convinced me to give it a try next time. (Or maybe help me improve my own simple Promise class. ) Cheers!
One nice thing about all these open source libraries is that you can always pick and choose parts of it to upgrade your own implementation as the need arises.
@mkatz I downloaded the sample project and found that it keeps showing an warning – “This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.”(iOS Deployment Target 10.0 & Xcode 8.3.3).
I think you probably should fix this by putting the > completion(result, nil) within DispatchQueue.main.async { … } in function getWeatherTheOldFashionedWay(latitude:, longitude:, completion:).
This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]