Basically, I have a structure called, topics which contains Title
, Description
and a Published
flag (see screenshot below for clarific
By using this function you can check any data that exist or not.
func checkUserExsistance(_ firURL : String ,_ childNode : String,_ value : String,_ ChildKey : String, completion : @escaping(Bool)->()){
let DBRef = Database.database().reference(fromURL: firURL)
let newDB = DBRef.child(childNode).queryOrdered(byChild: ChildKey).queryEqual(toValue: value)
newDB.observe(.value, with: { (snapPhot) in
print(snapPhot.value)
}) { (erooor) in
print(erooor)
}
}
You have a few small mistakes in there. Overall nothing too bad, but combined they'll never work:
query...
methods returns a new objectorderByChild()
before you can filter on its valueCombining these:
let ref = FIRDatabase.database().referenceFromURL(FIREBASE_URL).child("topics")
let query = ref.queryOrderedByChild("published").queryEqualToValue(true)
query.observeEventType(.Value, withBlock: { (snapshot) in
for childSnapshot in snapshot.children {
print(childSnapshot)
}
})
We get this question regularly. For example, this from yesterday looks very similar: Firebase Query not Executing Properly. Since my explanation varies with every answer, I recommend browsing a bit to read my relevant answers until it clicks.
self.ref = FIRDatabase.database().referenceFromURL(FIREBASE_URL).child("topics").
queryOrderedByChild("published").queryEqualToValue(true)
.observeEventType(.Value, withBlock: { (snapshot) in
for childSnapshot in snapshot.children {
print(snapshot)
}
})