Swift 3 and Firebase: Retrieve auto id value from snapshot
.value returns the parent node and all of the child nodes that satisfy the results of the query. In this case, the children would need to be iterated over (there could be more than 1 child that matches the query).
The snapshot.key is the parent node key (bS6JPkEDXIVrlYtSeQQgOjCNTii1) and the value is a snapshot (dictionary) of all of the children. Each of those children is a key:value pair of uid:value and value a dictionary of the child nodes, receiverId, senderId etc
This code will print the uid of each child node and one of the values, timestamp, for each user node that matches the query.
let usersRef = ref.child("users")
let queryRef = usersRef.queryOrdered(byChild: "receiverId")
.queryEqual(toValue: "LKupL7KYiedpr6uEizdCapezJ6i2")
queryRef.observeSingleEvent(of: .value, with: { (snapshot) in
for snap in snapshot.children {
let userSnap = snap as! FIRDataSnapshot
let uid = userSnap.key //the uid of each user
let userDict = userSnap.value as! [String:AnyObject]
let timestamp = userDict["timestamp"] as! String
print("key = \(uid) and timestamp = \(timestamp)")
}
})
How to get value from autoID in Firebase Database - Swift
The easiest way to get the count of guests who are marked as attending is to use a query:
let ref = Database.database().reference().child("userInfo").child(uid!)
let query = ref.child("guests").queryOrdered(byChild: "attending").queryEqual(toValue: "attending")
query.observe(.value) { snapshot in
let numberOfusers = snapshot.childrenCount
print(snapshot.childrenCount)
}
How to Firebase retrieve data below UID and auto ID In Swift
Right now your code is looping over the child nodes of /Users/Sellers
, which is the nodes of each individual user. What you seem to want to do is include all nodes under the product_list
property of each user, which requires an extra nested loop:
let refList = Database.database().reference().child("Users/Sellers")
refList.observe(DataEventType.value, with:{(snapshot) in
if snapshot.childrenCount>0{
self.listProduct.removeAll()
for user in snapshot.children.allObjects as! [DataSnapshot]{
let productList = user.childSnapshot(forPath: "product_list")
for product in productList.children.allObjects as! [DataSnapshot]{
let userList = product.value as? [String: AnyObject]
let listName = userList?["name"]
let listDetail = userList?["details"] // NOTE: also fixed a typo here
let listPrice = userList?["price"]
print("key = \(listName)")
let list = ListModel(name: listName as! String?, detail: listDetail as! String?, price: listPrice as! String?)
self.listProduct.append(list)
}
}
self.tableList.reloadData()
}
})
retrieving data from auto id database child
Since you have childByAutoId you have to use query ordered and query equal.
let reference = Database.database().reference().child("User_Informations").queryOrdered(byChild: "email")
reference.queryEqual(toValue: "ali_y_k@hotmail.com").observeSingleEvent(of: .childAdded) { (snapshot) in
let dictionary = snapshot.value as! [String : Any]
let firstName = dictionary["firstName"]
print(firstName)
}
Reading Data stored via Autoid from Firebase Swift
The way I usually do this is by:
ref.child("Phrases").observeSingleEvent(of: .value, with: { snapshot in
let value = snapshot.value as! [String:Any]
let name = value["phrase"] as? String ?? ""
})
Alternatively you could unwrap it first
ref.child("Phrases").observeSingleEvent(of: .value, with: { snapshot in
if let value = snapshot.value as? [String:Any] {
let name = value["phrase"] as? String ?? ""
}
})
Get information from Firebase Database without autoID
I think the confusion stems from the fact that you are observing .childAdded
. Since you're only observing one node/entity, you'll want to observe .value
instead:
Database.database().reference()
.child("users/BA917746-F5BE-4FA4-B23E-C998F4118CCE")
.observe(.value) { (snapshot) in
let dict = snapshot.value as! [String: Any]
self.name = dict["name"] as! String
If you we're to observe the entire users
node, that's when you'd either use .childAdded
or loop over the child nodes of the snapshot.
How can I retrieve data with queryEqualToValue in auto id child. Firebase-iOS
Given your realtime database looks something like this:
{
"students": {
1: {
"name": {
"first_name": "Nathapong",
"nick_name": "Oniikal3"
}
}
}
}
You can observe the students path with the event type ChildAdded
and order the query by child key name/first_name
. Then you can use queryEqualToValue
to find students with a particular first name.
let ref = FIRDatabase.database().referenceWithPath('students').queryOrderByChild("name/first_name").queryEqualToValue("Nathapong")
ref.observeSingleEventOfType(.ChildAdded, block: { snapshot in
print(snapshot)
})
Related Topics
How to Open Url in Safari and the Get Back to the App Under Uitests in Xcode 7
Migrating Data to App Groups Disables Icloud Syncing
Swiftui MACos Xcode Style Toolbar
Error Using Associated Types and Generics
Swift Error When Trying to Access Dictionary: 'Could Not Find Member 'Subscript''
Scenekit -- How to Get Animations for a .Dae Model
Convert Swift Dictionary to String
Swiftui: How to Draw Filled and Stroked Shape
Get Rawvalue from Enum in a Generic Function
Swift: Function with Default Parameter Before Non-Default Parameter
Swiftui Views with a Custom Init
How to Install Package in Xcode via Swift Package Manager
Adding Activity Indicator to Uialertview
Difference Between Swift's Hash and Hashvalue
Add Placeholder to Uitextfield, How to Set the Placeholder Text Programmatically in Swift