title: swift, alamofire cancel previous request
body: <p>I have an NetworkRequest class, where all my alamofire requests made:</p>
  
  <pre><code>    class NetworkRequest {
          static let request = NetworkRequest()
  
          var currentRequest: Alamofire.Request?
  
          let dataManager = DataManager()
          let networkManager = NetworkReachabilityManager()
          let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
  
          func downloadData&lt;T: Film&gt;(slug: String, provider: String, section: String, dynamic: String, anyClass: T, completion: ([T]?) -&gt; Void ) {
              var token: String = ""
  
              if LOGGED_IN == true {
                  token = "\(NSUserDefaults.standardUserDefaults().valueForKey(TOKEN)!)"
              }
  
              let headers = [
                  "Access": "application/json",
                  "Authorization": "Bearer \(token)"
              ]
  
              let dataUrl = "\(BASE_URL)\(slug)\(provider)\(section)\(dynamic)"
              print(headers)
              print(dataUrl)
  
              if networkManager!.isReachable {
  
                  currentRequest?.cancel()
  
                  dispatch_async(dispatch_get_global_queue(priority, 0)) {
  
                      if let url = NSURL(string: dataUrl) {
                          let request = Alamofire.request(.GET, url, headers: headers)
  
                          request.validate().responseJSON { response in
                              switch response.result {
                              case .Success:
                                  if let data = response.result.value as! [String: AnyObject]! {
                                      let receivedData = self.dataManager.parseDataToFilms(data, someClass: anyClass)
                                      completion(receivedData)
                                  }
  
                              case .Failure(let error):
                                  print("Alamofire error: \(error)")
  
                                  if error.code == 1001 {
                                      self.goToNoConnectionVC()
                                  }
  
                                  print("canceled")
                              }
  
                          }
                      }
                  }
              } else {
                  goToNoConnectionVC()
              }
          }
  }
  </code></pre>
  
  <p>And I need to cancel previous downloadData request, when the new one starts, tried to cancel using currentRequest?.cancel(), but it doesn't help. </p>
  
  <p>Already tried to cancelOperations using NSOperationsBlock, but it doesn't cancels current operation. </p>
  
  <p>I block UI now, so that user can't send another request. But this is not correct, causes some errors later...</p>
  
  <p>Pls, help</p>