Firebase删除用户后跳转欢迎页仍弹匿名提示的解决问询
Fix: Delete Account & Redirect to Welcome Screen Without Anonymous Prompt
Let's break down why you're seeing that annoying prompt after deleting the account, then fix it step by step:
What's Going Wrong
- Out-of-Order Operations: You're navigating to the WelcomeVC before deleting the database entry. Since Firebase operations are asynchronous, the app might reach the Welcome screen while the database delete is still in progress—leading to stale state checks that trigger the prompt.
- Incomplete Account Deletion: You're only removing the user's data from the Firebase Database, but not deleting the actual Firebase Auth user account. This leaves an active Auth session, so when the Welcome screen loads, your
firebaseCallCompletedlogic detects a user exists but can't find their database entry, triggering the "Anonymous user" alert. - Navigation Stack Bloat: Using
pushViewControlleradds the WelcomeVC to your existing navigation stack. If the user taps back, they'll end up back on the EditViewController, which isn't ideal for a post-deletion flow.
The Fix
Update your deleteByID function to handle deletion properly, wait for Firebase operations to complete, then navigate correctly:
@IBAction func deleteButton(_ sender: Any) { deleteByID() } func deleteByID(){ guard let currentUser = Auth.auth().currentUser else { // Edge case: No user logged in, skip to welcome screen navigateToWelcomeScreen() return } // 1. First delete the user's data from Firebase Database dataBaseRef.child("User").child(currentUser.uid).removeValue { [weak self] error, _ in guard let self = self else { return } if let error = error { print("Failed to delete user data: \(error.localizedDescription)") return } // 2. Then delete the Firebase Auth user account to end the session currentUser.delete { error in if let error = error { print("Failed to delete Auth account: \(error.localizedDescription)") return } // 3. Only navigate after both deletions are successful self.navigateToWelcomeScreen() } } } private func navigateToWelcomeScreen() { let storyboard = UIStoryboard(name: "Main", bundle: nil) let welcomeVC = storyboard.instantiateViewController(withIdentifier: "WelcomeVC") as! WelcomeVC // Reset the navigation stack so the user can't go back to EditViewController let navigationController = UINavigationController(rootViewController: welcomeVC) navigationController.modalPresentationStyle = .fullScreen self.present(navigationController, animated: true) // Alternative if you want to reuse the existing navigation stack: // self.navigationController?.setViewControllers([welcomeVC], animated: true) }
Key Changes Explained
- Async Completion Handling: We nest the Auth delete inside the database delete completion handler to ensure both operations finish before navigating—no more race conditions.
- Full Account Deletion: Calling
currentUser.delete()removes the active Auth session entirely. When the Welcome screen loads,Auth.auth().currentUserwill benil, so your prompt logic won't trigger. - Clean Navigation: Either presenting a fresh navigation stack with WelcomeVC as the root, or resetting the existing stack, ensures the user can't navigate back to the now-invalid EditViewController.
Quick Check for Your Welcome Screen
Make sure the logic that shows the prompt checks if a user is actually logged in and has valid database data. With the fix above, after deletion, there will be no active user session—so the prompt should never appear again.
内容的提问来源于stack exchange,提问作者iOSHG
相关产品推荐
相关产品推荐

