打印 Firebase 快照中具有特定子项和特定词的所有用户?
Printing all users in a Firebase Snapshot that have a specific Child with a Specific word?
"users" : {
"1ZWT7FAE2qThNQfBj7tbMO7BnMo1" : {
"Coordinates" : {
"latitude" : 50.054738,
"longitude" : 8.226809826085624
"Email" : mark@gmail.com
假设您有一个所有用户的子项(“电子邮件”)。现在你制作一个快照来检查所有用户的所有电子邮件。登录用户有一个@gmail 帐户。您打印所有与登录用户具有相同电子邮件域的用户,例如@gmail。可能像下面这样需要弄清楚粗线的地方
refArtists.observe(DataEventType.value,其中:{快照在
if snapshot.childrenCount>0{
self.users.removeAll()
for users in snapshot.children.allObjects as! [DataSnapshot] {
if users.key != thisUsersUid {
print("userskey",users.key)
let usersObject = users.value as? [String: AnyObject]
let usersEmail = peopleObject?["Email"] as? String
......
let A = users.childSnapshot(forPath: “Email)
**if A@ = the @ of thisUsersUid** {
let u = Userx(Email: peopleEmail, .........)
self.users.append(u)
} else {
print ("w")
}
} else {
print ("a")
}
}}
}
}
})
以下是我实际创建完整电子邮件的方式。这是在第一个答案之后添加的:
@IBAction func signInPressed(_ sender: Any) {
Auth.auth().createUser(withEmail: (emailField.text ?? ""), password: (passwordField.text ?? "")) { (user, error) in
if let _eror = error {
//something bad happning
print(_eror.localizedDescription )
let alert = UIAlertController(title: "Error", message: "Invalid Entry or Duplicate.", preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}else{
//user registered successfully
print(user as Any)
if let userID = user?.user.uid
{
KeychainWrapper.standard.set((userID), forKey: "uid")
let databaseRef = Database.database().reference()
databaseRef.child("people").child(userID).child("users").setValue(self.emailField.text!)
databaseRef.child("people").child(userID).child("postID").setValue(userID)
self.performSegue(withIdentifier: "tohome", sender: nil)
}
}
}
}
Firebase 不提供真正的部分字符串搜索 - 有方法可以搜索字符串的前面但无法搜索字符串的后面,例如您可以在此列表中搜索名字 steve
frank@mac.com
steve@gmail.com
larry@mac.com
但您无法搜索 gmail.com。
预先处理的方法是,当用户最初输入他们的电子邮件地址时,将其分解为代码中的各个组件;用户名,然后是域名,并将它们作为两个子注释存储在 Firebase
中
steve @ gmail.com
^ ^
username domain
那么您搜索 gmail.com 就很容易了。
您的用户节点现在看起来像这样
firebase
users
uid_0
email_name: "frank" //the logged in user
email_domain: "mac.com"
uid_1
email_name: "steve"
email_domain: "gmail.com"
uid_2
email_name: "larry"
email_domain: "mac.com"
然后获取与登录用户具有相同域的所有用户,这是 Firebase 代码。请注意,我们获取所有匹配的用户,然后从快照中删除当前用户。
func queryForDomainMatch() {
let myDomain = "mac.com" //read from my users node
let myUid = "uid_0" //the users uid - user 'frank' in this case
let ref = self.ref.child("users") //self.ref points to MY firebase
let query = ref.queryOrdered(byChild: "email_domain").queryEqual(toValue: myDomain)
query.observeSingleEvent(of: .value, with: { snapshot in
var allUsers = snapshot.children.allObjects as! [DataSnapshot]
if let index = allUsers.firstIndex(where: { [=13=].key == myUid } ) {
allUsers.remove(at: index) //remove the current user
}
for userSnap in allUsers {
let name = userSnap.childSnapshot(forPath: "email_name").value as! String
print(name)
}
})
}
输出将是匹配节点的名称
larry
编辑
要拆分电子邮件,只需对字符串使用 Swift 拆分功能。它将字符串分成用户名和域的数组。像这样
let email = "jay@mac.com"
let components = email.split(separator: "@")
components.forEach { print([=15=]) }
和输出
jay
mac.com
"users" : {
"1ZWT7FAE2qThNQfBj7tbMO7BnMo1" : {
"Coordinates" : {
"latitude" : 50.054738,
"longitude" : 8.226809826085624
"Email" : mark@gmail.com
假设您有一个所有用户的子项(“电子邮件”)。现在你制作一个快照来检查所有用户的所有电子邮件。登录用户有一个@gmail 帐户。您打印所有与登录用户具有相同电子邮件域的用户,例如@gmail。可能像下面这样需要弄清楚粗线的地方
refArtists.observe(DataEventType.value,其中:{快照在
if snapshot.childrenCount>0{
self.users.removeAll()
for users in snapshot.children.allObjects as! [DataSnapshot] {
if users.key != thisUsersUid {
print("userskey",users.key)
let usersObject = users.value as? [String: AnyObject]
let usersEmail = peopleObject?["Email"] as? String
......
let A = users.childSnapshot(forPath: “Email)
**if A@ = the @ of thisUsersUid** {
let u = Userx(Email: peopleEmail, .........)
self.users.append(u)
} else {
print ("w")
}
} else {
print ("a")
}
}}
}
}
})
以下是我实际创建完整电子邮件的方式。这是在第一个答案之后添加的:
@IBAction func signInPressed(_ sender: Any) {
Auth.auth().createUser(withEmail: (emailField.text ?? ""), password: (passwordField.text ?? "")) { (user, error) in
if let _eror = error {
//something bad happning
print(_eror.localizedDescription )
let alert = UIAlertController(title: "Error", message: "Invalid Entry or Duplicate.", preferredStyle: UIAlertController.Style.alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}else{
//user registered successfully
print(user as Any)
if let userID = user?.user.uid
{
KeychainWrapper.standard.set((userID), forKey: "uid")
let databaseRef = Database.database().reference()
databaseRef.child("people").child(userID).child("users").setValue(self.emailField.text!)
databaseRef.child("people").child(userID).child("postID").setValue(userID)
self.performSegue(withIdentifier: "tohome", sender: nil)
}
}
}
}
Firebase 不提供真正的部分字符串搜索 - 有方法可以搜索字符串的前面但无法搜索字符串的后面,例如您可以在此列表中搜索名字 steve
frank@mac.com
steve@gmail.com
larry@mac.com
但您无法搜索 gmail.com。
预先处理的方法是,当用户最初输入他们的电子邮件地址时,将其分解为代码中的各个组件;用户名,然后是域名,并将它们作为两个子注释存储在 Firebase
中 steve @ gmail.com
^ ^
username domain
那么您搜索 gmail.com 就很容易了。
您的用户节点现在看起来像这样
firebase
users
uid_0
email_name: "frank" //the logged in user
email_domain: "mac.com"
uid_1
email_name: "steve"
email_domain: "gmail.com"
uid_2
email_name: "larry"
email_domain: "mac.com"
然后获取与登录用户具有相同域的所有用户,这是 Firebase 代码。请注意,我们获取所有匹配的用户,然后从快照中删除当前用户。
func queryForDomainMatch() {
let myDomain = "mac.com" //read from my users node
let myUid = "uid_0" //the users uid - user 'frank' in this case
let ref = self.ref.child("users") //self.ref points to MY firebase
let query = ref.queryOrdered(byChild: "email_domain").queryEqual(toValue: myDomain)
query.observeSingleEvent(of: .value, with: { snapshot in
var allUsers = snapshot.children.allObjects as! [DataSnapshot]
if let index = allUsers.firstIndex(where: { [=13=].key == myUid } ) {
allUsers.remove(at: index) //remove the current user
}
for userSnap in allUsers {
let name = userSnap.childSnapshot(forPath: "email_name").value as! String
print(name)
}
})
}
输出将是匹配节点的名称
larry
编辑
要拆分电子邮件,只需对字符串使用 Swift 拆分功能。它将字符串分成用户名和域的数组。像这样
let email = "jay@mac.com"
let components = email.split(separator: "@")
components.forEach { print([=15=]) }
和输出
jay
mac.com