title: How does a view obtain data using a view model and Network API
body: <p>I'm trying to fetch some data with this helper file:
  <a href="https://gist.github.com/jbfbell/e011c5e4c3869584723d79927b7c4b68" rel="nofollow noreferrer">https://gist.github.com/jbfbell/e011c5e4c3869584723d79927b7c4b68</a></p>
  <p>Here's a snippet of the important code:</p>
  <p><b>Class</b></p>
  <pre><code>/// Base class for requests to the Alpha Vantage Stock Data API.  Intended to be subclasssed, but can
  /// be used directly if library does not support a new api.
  class AlphaVantageRequest : ApiRequest {
  
      private static let alphaApi = AlphaVantageRestApi()
      let method = &quot;GET&quot;
      let path = &quot;&quot;
      let queryStringParameters : Array&lt;URLQueryItem&gt;
      let api : RestApi = AlphaVantageRequest.alphaApi
  
      var responseJSON : [String : Any]? {
          didSet {
              if let results = responseJSON {
              print(results)
          }
      }
    }
  }
  </code></pre>
  <p><b> Extension ApiRequest </b></p>
  <pre><code>    /// Makes asynchronous call to fetch response from server, stores response on self
      ///
      /// - Returns: self to allow for chained method calls
      public func callApi() -&gt; ApiRequest {
          guard let apiRequest = createRequest() else {
              print(&quot;No Request to make&quot;)
              return self
          }
          let session = URLSession(configuration: URLSessionConfiguration.ephemeral)
          let dataTask = session.dataTask(with: apiRequest) {(data, response, error) in
              guard error == nil else {
              print(&quot;Error Reaching API, \(String(describing: apiRequest.url))&quot;)
              return
              }
              self.receiveResponse(data)
          }
          dataTask.resume()
          return self
      }
  </code></pre>
  
  
  My goal is to fetch the data from <b>responseJSON</b> after the data of the url request is loaded.
  <p>My ViewModel currently looks like this:</p>
  <pre><code>class CompanyViewModel: ObservableObject {
      
      var companyOverviewRequest: ApiRequest? {
          didSet {
              if let response = companyOverviewRequest?.responseJSON {
                  print(response)
              }
          }
      }
      
      private var searchEndpoint: SearchEndpoint
      
      init(companyOverviewRequest: AlphaVantageRequest? = nil,
           searchEndpoint: SearchEndpoint) {
          
          self.companyOverviewRequest = CompanyOverviewRequest(symbol: searchEndpoint.symbol)
      }
      
      
      func fetchCompanyOverview() {
          
          guard let request = self.companyOverviewRequest?.callApi() else { return }
          self.companyOverviewRequest = request
  
      }
      
  }
  </code></pre>
  <p>So in my ViewModel the didSet gets called once but not when it should store the data. The results of <strong>AlphaVantageRequest</strong> always prints out properly, but not in my ViewModel. How can I achieve to have the loaded data also in my ViewModel?</p>