ios - 当前 Firebase 用户返回 Nil
问题描述
我有一个使用 FirebaseAuth 对用户进行身份验证的应用。我在 AuthService 类中执行此操作;
import Foundation
import FirebaseAuth
class AuthService: NSObject {
private override init() {}
static let shared = AuthService()
let currentUser = Auth.auth().currentUser
var uid = Auth.auth().currentUser?.uid
func checkAuthState(completion: @escaping(FirebaseAuth.User?) -> Void) {
// let listener = Auth.auth().addStateDidChangeListener { (auth, user) in
Auth.auth().addStateDidChangeListener { (auth, user) in
switch user {
case .none:
print("USER NOT FOUND IN CHECK AUTH STATE")
completion(nil)
case .some(let user):
print("USER FOUND WITH ID: \(user.uid)")
completion(user)
}
}
// Auth.auth().removeStateDidChangeListener(listener)
}
}
然后我使用上面的 checkAuthState 函数匿名登录用户。一旦此操作与某个用户完成,我将执行一个操作来切换rootViewController
.
func checkAuthState() {
authService.checkAuthState { (user) in
switch user {
case .none:
SceneDelegate.shared.rootController.showWelcomeController()
case .some(_):
SceneDelegate.shared.rootController.showTabBarController()
}
}
}
}
这是我的代码RootViewController
;
func showWelcomeController() {
let welcomeController = WelcomeController(authService: AuthService.shared)
let navigationController = UINavigationController(rootViewController: welcomeController)
addChild(navigationController)
navigationController.view.frame = view.frame
view.addSubview(navigationController.view)
navigationController.didMove(toParent: self)
currentController.willMove(toParent: nil)
currentController.view.removeFromSuperview()
currentController.removeFromParent()
currentController = navigationController
}
func showTabBarController() {
let tabBarController = TabBarController()
addChild(tabBarController)
tabBarController.view.frame = view.frame
view.addSubview(tabBarController.view)
tabBarController.didMove(toParent: self)
currentController.willMove(toParent: nil)
currentController.view.removeFromSuperview()
currentController.removeFromParent()
currentController = tabBarController
}
以上所有操作都按预期运行并显示了我的TabBarController
,但是,然后我在我的 TabBarController 中的一个 ViewController 中运行一个函数,它是;
var uid = Auth.auth().currentUser?.uid
func readComments(query: Query, lastDocument: DocumentSnapshot?, completion: @escaping(Result<[CommentModel], NetworkError>) -> ()) {
guard let uid = auth.uid else { return completion(.failure(.userNotFound)) }
// Other code
}
但是,我收到 .userNotFound 错误?因为它返回零?我很困惑,因为 checkAuthState 正在返回用户?
解决方案
这通常意味着在currentUser
代码运行时尚未初始化。由于恢复用户的登录状态需要调用服务器,它是一个异步操作。
任何依赖于当前用户状态被恢复的代码都应该addStateDidChangeListener
在你的第一个片段中(或从中调用)。因此,您可能也需要在其他视图控制器中使用类似的片段。
推荐阅读
- glassfish - 引起:java.lang.IllegalArgumentException:Servlet [RegistrationRequesterPortImpl] 和 Servlet
- python - 附加在函数中的 Python 列表意外工作
- python - 如何在 Tkinter 中显示超长图像?(如何绕过画布最大限制)
- reporting-services - 如何在SSRS中用空格替换点?
- javascript - 此切换脚本适用于除 Internet Explorer 以外的所有浏览器,我该如何解决?
- python - 如何创建堆栈和队列?
- video - 如何使用 ffmpeg 或 imagemagick 将两个图像序列与 alpha 通道批量组合?
- c - 如何将字符串解析为结构?
- sql - 如何使用 SQL Server Management Studio 2008R2 从两台计算机上的两个表中求和
- android - 如何加快基于 ARM 的 AVD 的速度以使其真正可用?