Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当前Firebase用户返回零_Ios_Swift_Firebase Authentication - Fatal编程技术网

Ios 当前Firebase用户返回零

Ios 当前Firebase用户返回零,ios,swift,firebase-authentication,Ios,Swift,Firebase Authentication,我有一个使用FirebaseAuth对用户进行身份验证的应用程序。我在AuthService类中执行此操作 import Foundation import FirebaseAuth class AuthService: NSObject { private override init() {} static let shared = AuthService() let currentUser = Auth.auth().currentUse

我有一个使用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
var uid=Auth.Auth().currentUser?.uid
func readComments(查询:query,lastDocument:DocumentSnapshot?,完成:@escaping(Result)->()){
guard let uid=auth.uid else{返回完成(.failure(.userNotFound))}
//其他代码
}


但是,我得到了.userNotFound错误?当它返回零时?当checkAuthState返回用户时,我感到困惑?

这通常意味着
currentUser
在代码运行时尚未初始化。由于恢复用户的登录状态需要调用服务器,因此这是一个异步操作


任何依赖于正在还原的当前用户状态的代码都应该在
addStateDidChangeListener
中(或从中调用),就像在第一个代码片段中一样。因此,在其他视图控制器中也可能需要类似的代码段。

很有意义。将尝试此方法并返回!这可以使每个控制器中都有一个身份验证侦听器。正如你正确地说的,用户还没有被初始化。我的想法是,要么等到用户初始化后再显示下一个控制器,要么在Android上的每个控制器上都有侦听器(如果我需要删除后端的用户,这可能是最佳做法)。我看到开发人员将信息从一个活动传递到下一个活动,以防止需要使用另一个身份验证状态侦听器。但我个人总是使用身份验证状态侦听器,因为它也会迫使我考虑如果用户注销会发生什么(例如:禁用他们的帐户)。