Swift Firebase 检查用户是否存在
Swift Firebase Check if user exists
我做错了什么?我有一个类似于此图像中所示的数据库结构。
在 appleDelegate.swift 中,我只想检查某个用户令牌是否确实存在于 "users" 节点下。也就是说,如果 "users" 具有子 currentUserID(字符串标记)。我了解 observeSingleEvent 已执行 asynchronously.I 在 swift 中出现此错误:'Application windows are expected to have a root view controller at the end of application launch'。在 "func application(_ 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 加载给定位置的所有节点 - 一种用途是迭代它们以填充数据源。如果有 10,000 个用户,如果您观察 /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 如果用户节点存在则为真,否则为假,因此函数 presentUserViewController 将接受一个布尔值,然后根据用户类型设置 UI。
我做错了什么?我有一个类似于此图像中所示的数据库结构。
在 appleDelegate.swift 中,我只想检查某个用户令牌是否确实存在于 "users" 节点下。也就是说,如果 "users" 具有子 currentUserID(字符串标记)。我了解 observeSingleEvent 已执行 asynchronously.I 在 swift 中出现此错误:'Application windows are expected to have a root view controller at the end of application launch'。在 "func application(_ 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 加载给定位置的所有节点 - 一种用途是迭代它们以填充数据源。如果有 10,000 个用户,如果您观察 /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 如果用户节点存在则为真,否则为假,因此函数 presentUserViewController 将接受一个布尔值,然后根据用户类型设置 UI。