How to Query Firebase Data Childbyautoid

How to query firebase data childByAutoID?

If you know the locality value of the first item, this is possible with queryStarting(atValue:childKey:):

let query = ref.child("media")
.queryOrdered(byChild: "locality")
.queryStarting(atValue: "City of London", childKey: "-LgSsaqYzevONPTk2447")
.queryLimited(toLast: 50)

So the above orders all child nodes by their locality, and then starts returning results at the with with locality="City of London" and key -LgSsaqYzevONPTk2447.

How can I access Firebase data under a childByAutoID?

Here we go. An answer to the question. However, and I mentioned in my comment to the question, the structure could be improved. However, there may be more children under each listing child so this answer handles it.

This just print's each authors name but it shows how to get to it so the answer should be expanded for the other child nodes.

let listingsRef = self.ref.child("listings")
listingsRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let autoIdSnap = child as! DataSnapshot
for autoChild in autoIdSnap.children {
let childSnap = autoChild as! DataSnapshot
let dict = childSnap.value as! [String: Any]
let author = dict["author"] as! String
print(author)
}
}
})

Oh, and a better structure may be

listings
childByAutoId
author: "Some author"
listing: 9780184888
childByAutoId
author: "Another author"
listing: 9799292598

Edit: If there will only ever be one and only one childByAutoId under each listing, you can eliminate the loop in the above and do this

let listingsRef = self.ref.child("listings")
listingsRef.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let autoIdSnap = child as! DataSnapshot //each listing
let childDict = autoIdSnap.value as! [String: Any] //a dict of items within the listing
let firstKey = childDict.keys.first! //the key to the first item
let valuesDict = childDict[firstKey] as! [String: Any] //the values for that key
let author = valuesDict["author"] as! String //the author value
print(author)
}
})

Firebase Database Read childByAutoId

Database.database().reference(withPath: 
"childId").child(userId).observe(.childAdded)
{ (snapshot:DataSnapshot) in
// This will print all sub node
print(snapshot)
}

How to read the value of childByAutoId() in swift 4?

If you want to access the key of any particular data, you have to make a firebase query. And i believe that you have some unique value to identify your data.

For the purpose of getting the unique key, you can use this func :

func getUniqueFirebaseKey(){
let ref = Database.database().reference()
ref.child("CHILD_NAME")
.queryEqual(toValue: "UNIQUE_ID")
.observe(.value, with: { (snapshot: DataSnapshot) in`

if let snap = snapShot.value as? [String:Any] {

for key in snap.keys{
return key
}
})
}

By this you can get your auto ID key of firebase.

But the better option is to create a model and store that key while parsing the data from firebase.

How to query by value firebase?

You're missing an instruction to order the child nodes, which means that Firebase (by default) orders and filters on the priority of the nodes. The solution is to order by value and then filter:

refUsers.queryOrderedByValue().queryEqual(toValue: mediaUID).observeSingleEvent(of: .value, with: { (snapshot) in

Since you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. You will need to handle that in your code by iterating over the results:

for child in snapshot.children.allObjects as! [FIRDataSnapshot] {
print(child.value)
}

Firebase : Retrieve childByAutoID from Realtime Database

let postInfo = ["Description": txtPostDescription.text!, "ImageUrl": imgUrl, "Likes": 0]

var reference = FIRDatabase.database().reference().child("Posts").childByAutoId()

reference.setValue(postInfo)
let childautoID = reference.key
print(childautoID)

Note :- Althogh childByAutoId() is a great functionality from Firebase.But prefer timestamps to store the data into when you want to create a node with a unique key.The reason why i prefer timestamps is because they can also be helpful in sorting of data...But thats just me..

Alternative :-

let timeStamp = Int(NSDate.timeIntervalSinceReferenceDate()*1000) //Will give you a unique id every second or even millisecond if you want.. 
FIRDatabase.database().reference().child("Posts").child(timeStamp).setValue(postInfo)

Firebase: Accessing Data Stored via childByAutoID

I have the answer. To access the key of the randomly generated autoId, you have to pass it as a FIRDataSnapShot.

for snap in snapshot.children.allObjects {

let id = snap as! FIRDataSnapshot

print(id.key)

}

This will give you the randomly generated keys.

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 ?? ""
}
})


Related Topics



Leave a reply



Submit