调用两个API和构建结构-iOS-Swift

问题描述

我知道这很普遍,但是找不到答案。我想使用dispatchGroups,但不确定如何做

要求:API 1提供了id,我需要使用该id构造url并调用一个API来获取imageUrl,然后使用imageUrl构建struct

struct Item {
 let itemId: String?
 let quantity: String?
 let itemImageUrl: String?
}

struct InitialDetails: Codable {
  let id: String
  let quantity: String
}

struct ImagePathData: Codable {
 let imageUrl: String
}

API 1:

{ 
 items: [{
   id: "1",quantity: "10"
}]
}

API 2

{
itemImagePath: "https://itemizedUrl/fish.png"
}

代码

func fetchData() -> [Item] {
    URLSession.shared.dataTask(with: url) { (data,response,error) in
        var items: [Items] = []
        let initialData = try JSONDecoder().decode([InitialDetails].self,from: data)
        for info in initialData {
            var imageUrlPath: String?
            let imageDataUrl = "https://itemizedUrl.com/\(info.id)"
            URLSession.shared.dataTask(with: imageDataUrl) { (data,error) in
                imageUrlPath = try JSONDecoder().decode(ImagePathData.self,from data)
            }
            let item = Item(itemId: initialData.id,quantity: initialData.quantity,itemImageUrl: imageUrlPath)
            items.append(item)
        }
        return items
     }
}

我知道这是不对的。.pls建议如何实现这种情况

解决方法

您需要使用dispachGroup完成

func fetchData(completion:@escaping([Item]) -> ()) { 
  var items: [Items] = []
  let g = DispatchGroup()
  URLSession.shared.dataTask(with: url) { (data,response,error) in
  do {
  let initialData = try JSONDecoder().decode([InitialDetails].self,from: data)
  for info in initialData {
   g.enter() 
   let imageDataUrl = "https://itemizedUrl.com/\(info.id)"
   URLSession.shared.dataTask(with: imageDataUrl) { (data,error) in
     do {
     let imageUrlPath = try JSONDecoder().decode(ImagePathData.self,from data)
         let item = Item(itemId: initialData.id,quantity: initialData.quantity,itemImageUrl: imageUrlPath.imageUrl)
        items.append(item)
        g.leave()
        } catch { 
         print(error)
        }
   } 
  }
  } catch  {  
     print(error)
  }
  g.notify(queue:.main) {
    completion(items)
  }
 }
}