On my UIView, I have 3 UIImageViews to load 3 image’s url. As we all know, 3 UIImageView load images has time difference , But I have a requirement: After 3 images all downloaded, then 3 UIImageViews show these 3 images at the same time, how to do?
On my UIView, I have 3 UIImageViews to load 3 image’s url. As we all know, 3 UIImageView load images has time difference , But I have a requirement: After 3 images all downloaded, then 3 UIImageViews show these 3 images at the same time, how to do?
2
Answers
So if I understood correctly you want to firstly download all 3 images and only then display them? If so I’d suggest using a DispatchGroup.
Basically what this does is whenever starting a download you enter in a dispatch group and when the download is done you leave it. After every operation left the group you use the notify completion and display what you want.
If you are have minimum support version of iOS 13 and using Xcode 13.2, you can use
async
andawait
to achieve this:Source: SwiftLee: Async let explained: call async functions in parallel