我使用以下代码检查用户是否存在,然后从数据库中检索数据,否则不执行任何操作,但是在加载应用程序且用户不存在时(我是用户),它执行数据库查询给出
的错误在展开可选值:文件时意外发现nil
在此行
self.checksetup = document.get(“ setupComplete”)为!布尔
这应该不会发生,因为用户不存在,我正在Swift IOS中使用它,并使用了Firestore数据库和Firebase身份验证
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let user1: User = Auth.auth().currentUser!
if let user: User = Auth.auth().currentUser! {
let setupcheckref = Firestore.firestore().collection("users").document(Auth.auth().currentUser!.uid)
setupcheckref.getDocument{(document, error) in
if let document = document, document.exists{
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
self.checksetup = document.get("setupComplete") as! Bool
if self.checksetup == true {
if Auth.auth().currentUser!.isEmailVerified {
self.performSegue(withIdentifier: "toHomeScreen", sender: self)
SVProgressHUD.dismiss()
}
else{
print("please verify your email")
try! Auth.auth().signOut()
let alertController = UIAlertController(title: "Email Verification", message:
"Please verify your email to Login! Check Your Inbox!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
self.present(alertController, animated: true, completion: nil)
// self.view.makeToast("please verify your email")
self.sendEmailVerification()
SVProgressHUD.dismiss()
}
}
else{
self.view.makeToast("Please Setup Your Account!", duration: 2.5)
self.performSegue(withIdentifier: "tocompletesetup", sender: self)
SVProgressHUD.dismiss()
} }
}
// self.performSegue(withIdentifier: "toHomeScreen", sender: self)
}
}
答案 0 :(得分:3)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
guard let user: User = Auth.auth().currentUser else {
view.makeToast("Please Setup Your Account!", duration: 2.5)
performSegue(withIdentifier: "tocompletesetup", sender: self)
SVProgressHUD.dismiss()
return
}
let setupcheckref = Firestore.firestore().collection("users").document(user.uid)
setupcheckref.getDocument { document, _ in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
if self.checksetup = document.get("setupComplete") as? Bool, self.checksetup {
if user.isEmailVerified {
self.performSegue(withIdentifier: "toHomeScreen", sender: self)
SVProgressHUD.dismiss()
} else {
print("please verify your email")
do {
try? Auth.auth().signOut()
let alertController = UIAlertController(title: "Email Verification", message:
"Please verify your email to Login! Check Your Inbox!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
self.present(alertController, animated: true, completion: nil)
// self.view.makeToast("please verify your email")
self.sendEmailVerification()
SVProgressHUD.dismiss()
} catch {}
}
} else {
print("check failed",self.checksetup)
}
} else {
print("Document doesnt exists")
}
}
}