Pulling and refreshing data asynchronously in SwiftUI
Pull-to-refresh is an established touchscreen gesture to refresh a List
view. First implemented in Tweetie, one of the first iOS apps for Twitter, it is now part of the iOS SDK. Originally supported only in UIkit, iOS 15 offers support for it in SwiftUI, and as expected, it works well with the async
await
model.
In this recipe, we are going to implement an app that fetches a few random cryptocurrencies and presents them in a List
view.
Pulling the List
, the app fetches another sample of the currencies updating the List
. The network service we will use for this is called Random Data Generator (https://random-data-api.com). It offers several types of random data, as you can see from its help page:
The recipe fetches cryptocurrencies, but you can experiment with the same code using another endpoint.
Getting ready
Implement an iOS 15 SwiftUI app called RefreshableCrypto
.
How to do it…
As typical for an...