title: Alamofire + Combine + MVVM request example
body: <p>in my view controller I have a property <code>items</code> to which I have a subscription and rendering my view.</p>
  <p>For this view controller I have a view model where I have load method like:</p>
  <pre><code>    @Published private(set) var items: [Item] = []
  
      func load(params: [String: Any] = [:]) {
          self.isLoading = true
          
          self.subscription = WebRepo().index(params: params).sink(receiveCompletion: { [weak self] (completion) in
              switch completion {
              case .failure(let error):
                  print(&quot;Error is: \(error.localizedDescription)&quot;)
              default: break
              }
              self?.isLoading = false
          }, receiveValue: { [weak self] (response) in
              self?.items = response.data
          })
      }
  </code></pre>
  <p>and my <code>WebRepo</code> looks like:</p>
  <pre><code>final class WebRepo {
      func index(params: [String: Any]) -&gt; AnyPublisher&lt;MyResponse&lt;[Item]&gt;, Error&gt; {
          let url = URL(...)
          return AF.request(url, method: .get, parameters: params)
          .publishDecodable(type: MyResponse&lt;[Item]&gt;.self)
          .tryCompactMap { (response) -&gt; MyResponse&lt;[Item]&gt;? in
              if let error = response.error { throw error }
              return response.value
          }
          .eraseToAnyPublisher()
      }
  }
  </code></pre>
  <p>My user can load multiple times and as you can see It will subscribe each time when <code>load</code> method is called, which I think should not be like this.</p>
  <p>I tried to introduce property for my view model:</p>
  <pre><code>private var indexResponse: AnyPublisher&lt;MyResponse&lt;[Item]&gt;, Error&gt;?
  
  //And my load becomes
  
  func load(params: [String: Any] = [:]) {
     self.isLoading = true
     self.indexResponse = WebRepo().index(params: params)
  }
  </code></pre>
  <p>But in this case I can't make initial binding since initial value is <code>nil</code> hence it won't subscribe.</p>
  <p>Another question is about handling error from load, do I need to have property for <code>error</code> inside view model or is there way I can rethrow an error for <code>$items</code>?</p>