title: Attempt to present <UIAlertController: > on <ViewController:> whose view is not in the window hierarchy
body: <p>I am working on a project using ResearchKit, so I send the user into the task I create: </p>
  
  <pre><code>let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
  taskViewController.delegate = self
  presentViewController(taskViewController, animated: true, completion: nil)
  </code></pre>
  
  <p>When the user is finished with the survey, he goes into:</p>
  
  <pre><code>func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
      switch reason {
          case .Completed:
  </code></pre>
  
  <p>...
  }</p>
  
  <p>Here I encounter a problem when I try to show an alert before</p>
  
  <pre><code>taskViewController.dismissViewControllerAnimated(true, completion: nil)
  </code></pre>
  
  <p>I get the below error:</p>
  
  <p><em>Attempt to present UIAlertController: ... on ViewController: ... whose view is not in the window hierarchy</em></p>
  
  <p>Any idea of how I could present the alert before dismissing the ViewController?</p>
  
  <p>I use the below for the alert:</p>
  
  <pre><code>let alertView = UIAlertController(title: "Houps", message: "Could not connect to the server.", preferredStyle: .Alert)
  alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
  presentViewController(alertView, animated: true, completion: nil)
  </code></pre>
  
  <p><strong>EDIT</strong>:
  The code is as below:</p>
  
  <pre><code>if let httpResponse = response as? NSHTTPURLResponse {
                  print("HTTP response: \(httpResponse.statusCode)")
                  if httpResponse.statusCode == 201 {
                      taskViewController.dismissViewControllerAnimated(true, completion: nil)
                  }
              } else {
                  print("No HTTP response")
                  let alertView = UIAlertController(title: "Houps", message: "Could not connect to the server.", preferredStyle: .Alert)
                  alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
                  presentViewController(alertView, animated: true, completion: nil)
  
              }
  </code></pre>