title: How to process a result from Alamofire without ruining MVVM pattern
body: <p>So I recently started to migrate my code into MVVM pattern and I am having some issues.
  Currently I am working on my loginPage which authenticates the user.
  The problem is my i call my function, but my code doesn't wait for a response from it and moves to the next line. I think thats because of Alamofire's async stuff.
  Here is my code:</p>
  
  <pre class="lang-swift prettyprint-override"><code>// My login view controller
  class loginPageViewController: UIViewController, UITextFieldDelegate {
      private var viewModel = userViewModel()
      @IBOutlet weak var usernameTextField: UITextField!
      @IBOutlet weak var passwordTextField: UITextField!
      @IBAction func signInButton(_ sender: roundButton) {
          if(usernameTextField.text != "" &amp;&amp; passwordTextField.text != "") {
              self.viewModel.validate(nameField: self.usernameTextField.text!, passField: self.passwordTextField.text!)
              if(self.viewModel.isDone){
                  print("success")
                  self.presentPage(identifier: "homePageNavCtrl")
              }
          }
      }
      override func viewDidLoad() {
          super.viewDidLoad()
          usernameTextField.delegate = self
          passwordTextField.delegate = self
      }
  
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool{
          usernameTextField.resignFirstResponder()
          passwordTextField.resignFirstResponder()
          return true;
      }
  
      override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          self.view.endEditing(true)
      }
  }
  private extension loginPageViewController{
      func displayErrorMessage(errorMessage: String){
          let alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: UIAlertController.Style.alert)
          alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
          self.present(alert, animated: true, completion: nil)
      }
      func presentPage(identifier: String){
          let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
          let nextViewController = storyBoard.instantiateViewController(withIdentifier: identifier)
          self.present(nextViewController, animated:true, completion:nil)
      }
  }
  </code></pre>
  
  <p>What I want to do is when the sign In button is tapped, validate the user with a JSON request and move to the next page if credentials is correct.</p>
  
  <p>Here is my userViewModel</p>
  
  <pre class="lang-swift prettyprint-override"><code>class userViewModel{
      private var mng = restInterface()
      private var User = user()
      var isDone = false
      var id: Int {
          return User.ID
      }
      var username: String {
          return User.LOGINNAME
      }
      var password: String {
          return User.PASSHASH
      }
  }
  
  extension userViewModel{
      func updateId(id: Int){
          User.ID = id
      }
      func updateUsername(username: String){
          User.LOGINNAME = username
      }
  
      func updatePassword(password: String){
          User.PASSHASH = password
      }
  
      func validate(nameField: String, passField: String){
          let accountBaseURL = "myUrlLiesDownHere"
          var nm = nameField
          var ps = passField
          nm.insert("'", at: nm.startIndex)
          nm.insert("'", at: nm.endIndex)
          ps.insert("'", at: ps.startIndex)
          ps.insert("'", at: ps.endIndex)
          let params : [String: String] = ["sUser" : nm, "sPass" : ps, "sToken": "''"]
              self.mng.performRequest(URLString: accountBaseURL, parameters: params, method: .get, successCallback: { (dict) in
                  self.User.ID = dict.value(forKey: "ID") as! Int
                  self.User.LOGINNAME = dict.value(forKey: "LOGINNAME") as! String
                  self.User.PASSHASH = dict.value(forKey: "PASSHASH") as! String
                  self.loginDone()
              }) { (error) in
                  print("Error")
          }
      }
  
      func loginDone(){
          self.isDone = true
      }
  }
  </code></pre>
  
  <p>And here is my rest interface</p>
  
  <pre class="lang-swift prettyprint-override"><code>class restInterface {
      func performRequest(URLString: String, parameters : Parameters, method:HTTPMethod, successCallback: @escaping (NSDictionary) -&gt; Void, errorCallBack: @escaping (String) -&gt; Void) -&gt; Void{
  
          Alamofire.request(URLString, method: method, parameters: parameters).responseJSON { response in
              print(response)
              if let JSON = response.result.value as? [String: Any] {
                  print(JSON)
                  successCallback(JSON as NSDictionary)
              } else {
                  errorCallBack("JSON Doesn't Exist")
              }
          }
      }
  }
  </code></pre>
  
  <p>The isDone boolean on userViewModel must be true when I control it in my view controller. But It doesn't. When I hit the button once, result becomes true but nothing happens. When I hit it again, it redirects me to the next page which I want.
  So working with async really confused me, and I am digging for help here :) Thanks...</p>