authStateListener行为有缺陷,登录时显示错误的用户数据



已编辑

我的目标是让用户注册,当他们转到表视图时,上一个用户的数据不会显示。我的SceneDelegate中有一个主authStateListener。它存在于这样一个函数中:

func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.

// Use this method to undo the changes made on entering the background.
guard let windowScene = (scene as? UIWindowScene) else { return }

let window = UIWindow(windowScene: windowScene)
self.window = window
let auth = Auth.auth()

auth.addStateDidChangeListener { (_, user) in
if let usersignedin = user {
print(usersignedin.email)
db.document("student_users/(user?.uid)").getDocument { (docSnapshot, error) in
if error != nil {
print("(error)")
} else {
let docSnap = docSnapshot?.exists
guard docSnap! else {
let alreadyLoggedInAsASchoolViewController = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.SchoolEventDashboardStoryboardID) as! SchoolTableViewController
let navigationizedSchoolVC = UINavigationController(rootViewController: alreadyLoggedInAsASchoolViewController)
self.window!.rootViewController = navigationizedSchoolVC
self.window!.makeKeyAndVisible()
return
}

let alreadyLoggedInAsAStudentViewController = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.StudentEventDashboardStoryboardID) as! StudentSegmentedTableViewController
let navigationizedVC = UINavigationController(rootViewController: alreadyLoggedInAsAStudentViewController)
self.window!.rootViewController = navigationizedVC
self.window!.makeKeyAndVisible()

}
}
} else {
print("No user is signed in")
let notLoggedInAtAll = self.storyboard.instantiateViewController(withIdentifier: Constants.StoryboardIDs.GothereMainMenuStoryboardID) as! GothereMainMenuViewController
let navMainMenu = UINavigationController(rootViewController: notLoggedInAtAll)
self.window!.rootViewController = navMainMenu
self.window!.makeKeyAndVisible()
}
}

}    

这是我用来在应用程序启动时将用户导航到正确的vc,也用于推送通知处理。现在该函数工作正常,但当我以不同用户身份注销和登录时,前一个用户的数据会填充单元格。新用户注册后也是如此。

这是我用来填充表中单元格的函数View:

func getEventName() {
listener.documentListener = db.collection(Constants.Firebase.schoolCollectionName).order(by: "time_created", descending: true).addSnapshotListener(includeMetadataChanges: true) { (querySnapshot, error) in
if let error = error {
print("There was an error fetching the data: (error)")
} else {
self.events = querySnapshot!.documents.map { document in
return EventName(eventName: (document.get("event_name") as! String))
}
self.tableView.reloadData()
} 
}
}

路径的原始值运行良好:

static let schoolCollectionName = "school_users/(user?.uid)/events"

在用户注销之前,我还删除了documentListener

func logoutSelected() {

let alert = UIAlertController(title: "Logout", message: "Are you sure you want to logout?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
self.dismiss(animated: true, completion: nil)
}
let logoutAction = UIAlertAction(title: "Logout", style: .default) { (logoutAction) in
let firebaseAuth = Auth.auth()


self.listener.documentListener?.remove()
print("Document Listener removed.")

DispatchQueue.main.asyncAfter(deadline: .now()+1) {
do {
try firebaseAuth.signOut()
self.performSegue(withIdentifier: Constants.Segues.SchoolLogout, sender: self)
} catch let signOutError as NSError {
print("There was an error signing the user out. (signOutError)")
}
}


}
alert.addAction(cancelAction)
alert.addAction(logoutAction)
present(alert, animated: true, completion: nil)
}

现在,这只是一次发生的事情,当我重新运行模拟器时,当前用户的正确数据会显示出来,但是,生产中的用户无法理解发生了什么,所以完全消除它是唯一的选择。

我想不出任何其他方法来解决这个问题,如果有任何帮助,我将不胜感激,谢谢。

好的,所以基本上我所做的是从路径中删除Constants值,现在它工作得很好,但该应用程序仍然有缺陷,有时它会允许我点击一些按钮,有时它不会。

最新更新