[英]Fetching user's data from firebase?
Currently I'm displaying user's contacts this way 目前,我正在以这种方式显示用户的联系人
ref.observe(.value, with: { snapshot in
var newItems: [Contacts] = []
for item in snapshot.children {
let contact = Contacts(snapshot: item as! FIRDataSnapshot)
newItems.append(contact)
}
// filter the data here
let userID = FIRAuth.auth()?.currentUser?.email
let itemsMatching = self.items.filter {
$0.addedByUser == userID
}
self.items = newItems
let sortedNames = itemsMatching.sorted { $0.lastName < $1.lastName }
self.tableView.reloadData()
})
How can I display only users with same email as in column addedByUser? 如何仅显示与addedByUser列中具有相同电子邮件的用户? When u create a new contact it will automatically assign value of your email to addedByUser.
当您创建新的联系人时,它将自动将您的电子邮件的值分配给addByUser。 So for example if I add contact from account show@mycontact.com it will display only contacts with value addedByUser: "show@mycontact"
因此,例如,如果我从帐户show@mycontact.com添加联系人,它将仅显示具有添加值的联系人。ByUser:“ show @ mycontact”
Updated answer - with two options :-) 更新的答案-有两个选项:-)
let ref = FIRDatabase.database().reference(withPath: "contacts")
var newItems : [ContactItem] = []
ref.observe(.value, with: { snapshot in
for item in snapshot.children
{
let item = ContactItem(snapshot: item as! FIRDataSnapshot)
newItems.append(item)
}
let userID = FIRAuth.auth()?currentUser?.email
let itemsMatching = newItems.filter
{
$0.addedByUser == userID
}
let sortedNames = itemsMatching.sorted { $0.lastName < $1.lastName }
print(sortedNames)
})
On the other hand, given that you're iterating through the snapshot anyway, you could always go for the simple approach 另一方面,考虑到仍然要遍历快照,您总是可以采用简单的方法
let ref = FIRDatabase.database().reference(withPath: "contacts")
var newItems : [ContactItem] = []
let userID = FIRAuth.auth()?currentUser?.email
ref.observe(.value, with: { snapshot in
for item in snapshot.children
{
let item = ContactItem(snapshot: item as! FIRDataSnapshot)
if item.addedByUser == userID
{
newItems.append(item)
}
}
let sortedNames = newItems.sorted { $0.lastName < $1.lastName }
print(sortedNames)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.