title: To cancle any api request Using Alamofire in swift
body: <p>I am calling api using alamofire in swift.it is a search api, when I am searching any text in searchbar I am calling the api. I am calling the api after a delay of 0.75 seconds. I want to cancel the previous request, when new request is there. But I don’t know how to cancel the request. Can anyone help me?</p>
  <pre><code> func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
          NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.reload(_:)), object: searchBar)
          perform(#selector(self.reload(_:)), with: searchBar, afterDelay: 0.75)
      }
  
  
  
    @objc func reload(_ searchBar: UISearchBar) {
       
          guard let query = searchBar.text, query.trimmingCharacters(in: .whitespaces) != &quot;&quot; else {
              self.searchLcodeWithText(&quot;&quot;)
              
              print(&quot;nothing to search&quot;)
              return
          }
          self.searchLcodeWithText(query)
          
          print(query)
      }
  
   private func searchLcodeWithText(_ newText: String){
          //Show
          startActivityIndicator()
          apiService.searchlcode(searchText: newText) { [self] (lcodeData) in
              allLcodeArray = []
              //Hide
              if let lcde = lcodeData{
                 
                  if lcde.count &gt; 0{
                
                      allLcodeArray.append(contentsOf: lcde)
                      stopActivityIndicator()
                  }
                
              }
              else{
                 
              }
              stopActivityIndicator()
              DispatchQueue.main.async {
                  self.tableView.reloadData()
              }
          }
      }
  }
  
  </code></pre>
  <pre><code>
  import Foundation
  import Alamofire
  
  class ApiService{
  
    func searchlcode(searchText: String, completion: @escaping searchLcodeTypeAliasCompletion){
          
          guard let urlrequest = URL(string: SEARCH_URL) else {return}
          var parameters = [String:Any]()
          parameters.updateValue(searchText, forKey: &quot;lcode_name&quot;)
          Alamofire.request(urlrequest, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).responseJSON { [self] (response : DataResponse&lt;Any&gt;) in
              if let error = response.result.error {
                  debugPrint(error.localizedDescription)
                  return
              }else{
              
              guard let data = response.data else { return completion(nil)}
              let jsonDecoder = JSONDecoder()
              do {
                  
                  let searchUser = try jsonDecoder.decode(searchLcodeTypeAlias.self, from: data)
                  print(searchUser)
                  completion(searchUser)
              } catch {
                  debugPrint(error)
                  completion(nil)
              }
          }
          }
          
      }
  
  }
  
  </code></pre>