Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
Swift Firebase检查用户是否存在_Swift_Firebase_Firebase Realtime Database_Completionhandler - Fatal编程技术网

Swift Firebase检查用户是否存在

Swift Firebase检查用户是否存在,swift,firebase,firebase-realtime-database,completionhandler,Swift,Firebase,Firebase Realtime Database,Completionhandler,我做错了什么?我有一个数据库结构,如图所示 在appleDelegate.swift中,我只想检查“users”节点下是否存在某个用户令牌。也就是说,如果“用户”具有子currentUserID(字符串标记)。据我所知,observeSingleEvent是异步执行的。我在swift中发现此错误:“应用程序启动结束时,应用程序窗口应具有根视图控制器”。在“func应用程序(application:UIApplication)”中,我有以下代码。下面还有我的完成处理程序函数 if let user

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

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。从那里确定这是否是现有用户,并将用户发送到相应的UI

.observeSingleEvent加载给定位置的所有节点-一个用途是对它们进行迭代以填充数据源。如果有10000个用户,则如果观察/users节点,它们都将被加载

在这种情况下,这真的没有必要。最好只观察您感兴趣的单个节点,如果它存在,将用户发送到现有用户的UI

下面是实现这一点的代码

    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;如果用户节点不存在,snapshot.exists将为false,因此函数presentUserViewController将接受bool,然后根据用户类型设置UI。

有关错误,请检查此项。我不确定在加载根视图控制器之前读取应用程序委托中的数据库是否是最佳做法。我可以错误,但你的应用程序正在尝试等待Firebase返回值,然后你的应用程序才有根视图控制器。如果你给应用程序一个根视图控制器,然后检查数据,它是否给你同样的错误?如果没有,那么你需要重新构造你的应用程序。嗯……这个想法是在美国er存在于“users”节点下…它应该像自动登录一样…如果在“users”节点中找不到currentUserId,则将用户发送到登录/注册控制器。我以编程方式完成所有操作,但也许我可以将代码从applegate中取出,将applegate链接到某种“启动屏幕”viewcontroller并在那里进行检查?我将使用@temp_uu来执行此功能。AppDelegate不是执行此功能的最佳位置,而且您所指的是一个rootViewController和一个尚不存在的self.window。此外,所有这些代码都可以替换为6行。最后,您为什么不直接读取users/uid节点directly而不是加载10000个用户并检查列表中是否有uid子节点?这会更快,代码也会更少..我不知道observesingleevent加载所有用户只是为了检查uid子节点是否存在..请告诉我检查“用户”下是否存在指定uid的正确方法.请详细说明“该代码可以用6行代码替换”明白了吗…有道理。