trying to understand the asynchronous call behaviour and main queue in swift
<p>I have an <code>Authenticator</code> class having <code>sendEmailForPasswordRecovery</code>  method implementing <code>asynchronous</code> call that sends the email to <code>firebase</code> user for password recovery.</p>

<pre><code>func sendEmailForPasswordRecovery(email: String, completion: CallBackWithError) {
        FIRAuth.auth()?.sendPasswordResetWithEmail(email, completion: { (error: NSError?) in
            completion(error)
        })
    }
</code></pre>

<p>I am calling this <code>function</code> from a <code>UIViewController</code></p>

<pre><code>Authenticator().sendEmailForPasswordRecovery(email, completion: { (error: NSError?) in
      print("operation completed. error: \(error)")
      self.completion?()
})
</code></pre>

<p>The completion blocks simply calls this function. It simply <strong>hides</strong> a <strong>popUp</strong> view, and <strong>fadeout</strong> the <strong>blurEffect</strong> and <strong>removes</strong> it from its <strong>parent view</strong>.</p>

<pre><code>func removeForgotPasswordScreen() {
        UIView.animateWithDuration(0.5, animations: { 
            self.blurEffectView.alpha = 0
            self.containerForEmail.alpha = 0
        }) { (_: Bool) in
            self.containerForEmail.hidden = true
            self.blurEffectView.removeFromSuperview()
        }
    }
</code></pre>

<p>but when <code>Authenticator().sendEmailForPasswordRecovery</code> is executed I can see that the error is nil in console. But the <strong>popUp</strong> View is disappearing only after 40-50 second later. But when I wrap the completion inside <code>dispatch_async</code>, I get my result immidiately.</p>

<pre><code>Authenticator().sendEmailForPasswordRecovery(email, completion: { (error: NSError?) in
   //   self.completion?() &lt;----- this was causing delay

   dispatch_async(dispatch_get_main_queue(), {
          self.completion?() &lt;------ Now it updates immidiately
    })
})
</code></pre>

<p>Firebase <code>sendPasswordResetWithEmail</code> has signature:</p>

<pre><code>public func sendPasswordResetWithEmail(email: String, completion: FIRSendPasswordResetCallback?)
</code></pre>

<p>and it says </p>

<blockquote>
  <p>@param completion Optionally; a block which is invoked when the
  request finishes. Invoked
              asynchronously on the main thread in the future.</p>
</blockquote>

<p>What I dont Understand is why did the PopUp was disappearing after certain delay in the first place and How did the <code>dispatch_assync</code> did the job immidiately.</p>