Fetching remote data using Combine and visualizing it in SwiftUI
A common characteristic that most mobile apps have is that they fetch data from a remote web service. Given the asynchronous nature of the problem, it is often problematic when this is implemented in the normal imperative world. However, it suits the reactive world nicely, as we'll see in this recipe.
We are going to implement a simple weather app, fetching the current weather and a 5-day forecast from OpenWeather, a famous service that also has a free tier.
After fetching the forecast, we will present the results in a list view, with the current weather fixed on the top.
Getting ready
We will start by creating a SwiftUI app called Weather
.
To use this service, we must create an account on OpenWeather:
- Go to the OpenWeather signup page (https://home.openweathermap.org/users/sign_up) and fill in your data:
- After confirming...