title: How to guarantee the correct order in async request
body: <p>I have a iOS application that use Alamofire to make URL requests. I sometimes find the requests arriving in the wrong order when there is very little time between them. This has to do with the nature of async requests as I understand. Is there any way to guarantee the correct order of the requests? I've been thinking you could wait for each request to finish because you have a completion handler or maybe you could handle this at the server side with a timestamp on each request so the server can discard requests that has a lower timestamp. I don't know what's the best solution though. </p>
  
  <p>My code so far:</p>
  
  <pre><code>Alamofire.request(
       defaults.string(forKey: "APIurl")! + path,
       method: httpMethod,
       parameters: parameters,
       encoding: JSONEncoding.default,
       headers: headers).responseJSON
       { response in
  
       // Check if the request was successful
       var success = false
       if (response.result.isSuccess) {
           let statusCode = response.response!.statusCode
           if (statusCode == 200) {
              success = true
          } else {
              showAlert("COULD_NOT_COMPLETE_TASK_TITLE", message: "TRY_AGAIN_LATER")
          }
      }
  
  }
  </code></pre>
  
  <p>I use sliders to change a value between 0 and 100. So in my case the order of the requests are crucial. Let's say I change a slider from 50 to 60. With async requests it sometimes execute first 60 and then 50. This is an issue as it's sent to my API that way and saves the latest value (in this case 50) in the database even though my desired value was 60.</p>