我有一个Authenticator
类具有sendEmailForPasswordRecovery
方法实施asynchronous
呼叫发送电子邮件给firebase
用户密码恢复。试图了解异步通话行为和主队列在快速
func sendEmailForPasswordRecovery(email: String, completion: CallBackWithError) {
FIRAuth.auth()?.sendPasswordResetWithEmail(email, completion: { (error: NSError?) in
completion(error)
})
}
我打电话这个function
从UIViewController
Authenticator().sendEmailForPasswordRecovery(email, completion: { (error: NSError?) in
print("operation completed. error: \(error)")
self.completion?()
})
完成块简单地调用这个函数。它只是隐藏一个弹出图,淡出的blurEffect和移除从其父视图。
func removeForgotPasswordScreen() {
UIView.animateWithDuration(0.5, animations: {
self.blurEffectView.alpha = 0
self.containerForEmail.alpha = 0
}) { (_: Bool) in
self.containerForEmail.hidden = true
self.blurEffectView.removeFromSuperview()
}
}
,但在执行Authenticator().sendEmailForPasswordRecovery
我可以看到,错误是在控制台为零。但是,视图在40-50秒之后才会消失。但是当我在dispatch_async
内完成完成时,我立刻得到了我的结果。
Authenticator().sendEmailForPasswordRecovery(email, completion: { (error: NSError?) in
// self.completion?() <----- this was causing delay
dispatch_async(dispatch_get_main_queue(), {
self.completion?() <------ Now it updates immidiately
})
})
火力地堡sendPasswordResetWithEmail
有签名:
public func sendPasswordResetWithEmail(email: String, completion: FIRSendPasswordResetCallback?)
和它说
@param完成可选;当 请求结束时调用的块。在将来的主线程上异步调用 。
我不明白的是为什么PopUp在第一个延迟后消失了,dispatch_assync
是怎么做的。
一个快速建议 - 我推荐阅读关于Grand Central Dispatch(GCD),这是iOS开发中用于并发处理的系统。 –
你什么时候设置完成变量 - 你确定它在第一种情况下设置正确吗?在removeForgotPasswordScreen中添加一个断点,以查看何时实际调用了该断点以及从哪个线程。 – fishinear
@MattLeFleur感谢您的建议。这非常有帮助。 – Ccr