func downloadImage()

in performance/SwiftUIPerformanceQuickstart/PerformanceExample/Shared/Process.swift [213:243]


  func downloadImage() {
    updateStatus(to: .running(.download))
    guard let url = URL(string: site)
    else {
      updateStatus(to: .failure(.download))
      print("Failure obtaining URL.")
      return
    }
    let download = URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
      guard error == nil else {
        print("Error encountered during download: \(String(describing: error))")
        self?.updateStatus(to: .failure(.download))
        return
      }
      guard let httpResponse = response as? HTTPURLResponse,
        (200 ... 299).contains(httpResponse.statusCode) else {
        print("Did not receive acceptable response: \(String(describing: response))")
        self?.updateStatus(to: .failure(.download))
        return
      }
      guard let mimeType = httpResponse.mimeType, mimeType == "image/png", let data = data,
        let image = UIImage(data: data) else {
        print("Something went wrong.")
        self?.updateStatus(to: .failure(.download))
        return
      }
      self?.updateStatus(to: .success(.download))
      self?.updateImage(to: image)
    }
    download.resume()
  }