(note: I'm a beginner to programming) I just have a semantic question regarding Combine. I was under the impression that Combine was always required for serial HTTP requests but while experimenting I found that the following worked just fine:
// this function works even though it depends on the output of the first HTTP request
@available(iOS 15.0, *)
func displayPictures(completion: @escaping ([(AsyncImage<Image>, UUID)]) -> Void) throws {
do {
try getPictures { urls in
var tempImageArr = [(AsyncImage<Image>, UUID)]()
for url in urls {
guard let url = URL(string: url) else {
print("Invalid URL")
return
}
let image = AsyncImage(url: url)
let id = UUID()
tempImageArr.append((image, id))
}
completion(tempImageArr)
}
} catch {
throw NetworkError.failedToGetPictures
}
}
func getPictures(completion: @escaping ([String]) -> Void) throws {
guard let url = URL(string: "https://randomuser.me/api/?results=10&inc=picture") else {
print("Invalid URL")
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
let decoder = JSONDecoder()
let decodedResponse = try! decoder.decode(RandomUserModel.self, from: data!)
completion(decodedResponse.pictures)
}.resume()
}
I'm hoping someone can clear up my confusion.
Thanks everyone.