title: Consume data from blockchain API
body: <p>I'm not sure what I'm doing wrong, I've been at it for an hour, I'm trying to print 1 or 2 lines from</p>
  <p><a href="https://api.covalenthq.com/v1/1/address/0x8f299f2908c9Cd71e723E7059Ac52eaea3638b2E/balances_v2/?&amp;key=ckey_4eeea29a22c14701a9844f01151" rel="nofollow noreferrer">https://api.covalenthq.com/v1/1/address/0x8f299f2908c9Cd71e723E7059Ac52eaea3638b2E/balances_v2/?&amp;key=ckey_4eeea29a22c14701a9844f01151</a></p>
  <p>but I get an error. I tried to debug it and it seems like my model and it's call is wrong, what's going on?</p>
  <pre><code>import SwiftUI
  
  struct FetchingAPI: View {
      
      @State private var results = [Result]()
      
      var body: some View {
          Text(&quot;Fetching API&quot;)
              .onAppear(perform: loadData)
      }
      
      var semaphore = DispatchSemaphore (value: 0)
      
      
      func loadData() {
          guard let url = URL(string: &quot;https://api.covalenthq.com/v1/1/address/0x8f299f2908c9Cd71e723E7059Ac52eaea3638b2E/balances_v2/?&amp;key=ckey_4eeea29a22c14701a9844f01151&quot;) else {
              print(&quot;Invalid URL&quot;)
              return
          }
          
          let request = URLRequest(url: url, timeoutInterval: Double.infinity)
          
          
          let task = URLSession.shared.dataTask(with: request) { data, response, error in
              if let data = data {
                  
                  print(String(data: data, encoding: .utf8)!)
                  
                  if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
                      // AFTER HERE THE DATA IS [] NON EXISTENT
                      DispatchQueue.main.async {
                          // update our UI
                          self.results = decodedResponse.results
                      }
                      
                      // everything is good, so we can exit
                      return
                  }
              } else {
                  print(String(describing: error))
                  semaphore.signal()
                  return
              }
              semaphore.signal()
          }
          
          task.resume()
          semaphore.wait()
      }
      
      struct Response: Codable {
          var results: [Result]
      }
      
      struct Result: Codable {
          var data: Int
          var address: String
          var quote_currency: String
      }
      
  }
  </code></pre>
  <p><strong>EDIT: Adding error message after adding suggested try catch:</strong></p>
  <p>I updated the code to be:</p>
  <pre><code>do {
      if let data = data {
          //  print(String(data: data, encoding: .utf8)!)
          let decodedResponse = try JSONDecoder().decode(Response.self, from: data)
          // we have good data – go back to the main thread
          DispatchQueue.main.async {
              // update our UI
              self.results = decodedResponse.results
          }
          
          // everything is good, so we can exit
          return
      }
      
  } catch {
      print(&quot;Unexpected error: \(error).&quot;)
      semaphore.signal()
      return
  }
  </code></pre>
  <p>ERROR Message:</p>
  <pre><code>Unexpected error: keyNotFound(CodingKeys(stringValue: &quot;updatedAt&quot;, intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: &quot;data&quot;, intValue: nil)], debugDescription: &quot;No value associated with key CodingKeys(stringValue: \&quot;updatedAt\&quot;, intValue: nil) (\&quot;updatedAt\&quot;).&quot;, underlyingError: nil)).
  </code></pre>