Firebase检查用户是否存在

Firebase检查用户是否存在

本文介绍了Swift Firebase检查用户是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么错?我有一个数据库结构,如该图中所示.
在appleDelegate.swift中,我只想检查"users"节点下是否确实存在某个用户令牌.也就是说,如果用户"具有子currentUserID(字符串令牌).我知道observeSingleEvent是异步执行的.我很快就收到此错误:应用程序窗口应在应用程序启动结束时具有一个根视图控制器".在"func application(_ application:UIApplication)"中,我有此代码.下面也有我的完成处理函数.

What am i doing wrong? I have a database structure like the one shown in this image.
In appleDelegate.swift i just want to check if a certain user token actually exists under the "users" node. that is, if "users" has the child currentUserID (a string token). I understand observeSingleEvent is executed asynchronously.I get this error in swift: 'Application windows are expected to have a root view controller at the end of application launch'. in "func application(_ application: UIApplication" i have this code. I also have my completion handler function below.

if let user = Auth.auth().currentUser{
        let currentUserID = user.uid
        ifUserIsMember(userId:currentUserID){(exist)->() in
            if exist == true{
                print("user is member")
                self.window?.rootViewController = CustomTabBarController()
            } else {
                self.window?.rootViewController = UINavigationController(rootViewController: LoginController())
            }
        }

        return true
    } else {
        self.window?.rootViewController = UINavigationController(rootViewController: LoginController())
        return true
    }
}

func ifUserIsMember(userId:String,completionHandler:@escaping((_ exists : Bool)->Void)){
    print("ifUserIsMember")
    let ref = Database.database().reference()
    ref.child("users").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.hasChild(userId) {
            print("user exists")
            completionHandler(true)
        } else {
            print("user doesn't exist")
            completionHandler(false)
        }
    })
}

推荐答案

我建议将代码从应用程序委托中移出,并移至初始viewController中.从那里确定这是否是现有用户,然后将该用户发送到相应的用户界面.

I would suggest moving the code out of the app delegate and into an initial viewController. From there establish if this is an existing user and send the user to the appropriate UI.

.observeSingleEvent在给定位置加载所有节点-一种用途是在它们上进行迭代以填充数据源.如果观察到/users节点,则如果有10,000个用户,则将全部加载进来.

.observeSingleEvent loads all of the nodes at a given location - one use would be to iterate over them to populate a datasource. If there were 10,000 users they would all be loaded in if you observe the /users node.

在这种情况下,实际上是没有必要的.最好只观察您感兴趣的单个节点,如果有,将用户发送到现有用户的UI.

In this case it's really not necessary. It would be better to just observe the single node you are interested in and if it exists, send the user to a UI for existing users.

这是执行此操作的代码

    if let user = Auth.auth().currentUser {
        let ref = self.ref.child("users").child(user.uid)
        ref.observeSingleEvent(of: .value, with: { snapshot in
            self.presentUserViewController(existing: snapshot.exists() )
        })
    }

snapshot.exists如果存在用户节点,则为true;否则为false,因此功能presentUserViewController会接受布尔值,然后根据用户类型来设置UI.

snapshot.exists will be either true if the user node exists or false if not so the function presentUserViewController would accept a bool to then set up the UI depending on the user type.

这篇关于Swift Firebase检查用户是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:39