Getting Started with Async/Await in iOS
When working with asynchronous code we often leverage the use of callbacks so we can execute code when the asynchronous operation finishes. This works fine in simple scenarios but gets complicated if we have to perform a future request based on the result of the previous request. The callback pattern also open doors for not remembering to execute the user interface code on the main thread, which can lead to performance issues.
Swift 5.5 includes a new way of performing asynchronous actions. This is done by the support of async/await feature. In this article we will look at how you can get started with using async/await in your iOS applications.
Invoking Movies API Using Callbacks
Before jumping into the async/await details, let’s take a look at how we currently fetch data from an API using callbacks. For this example we will be using the OMDB API to fetch a list of movies. The code below shows the getMovies function inside the Webservice class, which is responsible for making a network call and fetching all the movies from the OMDB API.