title: How to cancel multiple networking requests using Moya
body: <p>I am currently using <a href="https://github.com/Moya/Moya" rel="noreferrer">Moya</a> to structure my networking calls. Per their <a href="https://github.com/Moya/Moya/blob/master/docs/Examples/Basic.md" rel="noreferrer">docs</a>, I have configured it as the following:</p>
  
  <pre><code>enum SomeAPIService {
      case endPoint1(with: Object)
      case endPoint2(duration: Int)
  }
  </code></pre>
  
  <p>When calling an endpoint (in this case, <code>endPoint1</code>), I do the following:</p>
  
  <pre><code>let provider = MoyaProvider&lt;SomeAPIService&gt;()
  provider.request(.endPoint1(object: object)) { (result) in
  switch result {
      case let .success(moyaResponse):
          finished(Result.success(value: moyaResponse.value))
      case let .failure(error):
          let backendError = BackendError(localizedTitle: "", localizedDescription: "Some error", code: moyaResponse.statusCode)
          finished(Result.failure(error: backendError))
      }
  })
  </code></pre>
  
  <p>My goal is, upon the user performing an action, <strong>cancel all the networking requests</strong> that's happening. </p>
  
  <p>Accordingly, Moya does allow one to cancel requests from the discussion <a href="https://github.com/Moya/Moya/issues/1023" rel="noreferrer">here</a>. From the most upvoted comment, someone mentioned <code>let request_1 = MoyaRequestXXXXX</code> and then <code>ruest_1.cancel()</code></p>
  
  <p>My problem is: </p>
  
  <ol>
  <li>How would I keep pointer to the requests?</li>
  <li><code>provider</code> doesn't have a <code>cancel()</code> function - so how should I be calling it?</li>
  </ol>
  
  <p>Any help is much appreciated.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Per the helpful suggestion about using <code>[Cancellable]</code>, I did the following:</p>
  
  <p>(1) In my app's singleton instance called <code>Operator</code>, I added <code>var requests = [Cancellable]()</code>
  (2) Every API call is added to the <code>requests</code> array as a Cancellable, like so:</p>
  
  <pre><code>let provider = MoyaProvider&lt;SomeAPIService&gt;()
  Operator.shared.requests.append(provider as! Cancellable) //causing error
  provider.request(.endPoint1(object: object)) { (result) in
  //rest of the block omitted 
  </code></pre>
  
  <p>I think I am not getting the syntax correct, and am adding the provider and not the request. However, since the request is itself a block, where would be the place to add the request? </p>