Basically, I have a structure called, topics which contains Title
, Description
and a Published
flag (see screenshot below for clarification ).
In the application, I want to filter the data and only show the topics that have published = true
.
This is what I'm trying to do:
self.ref = FIRDatabase.database().referenceFromURL(FIREBASE_URL).child("topics")
self.ref?.queryEqualToValue("published")
self.ref?.observeEventType(.Value, withBlock: { (snapshot) in
//...Handle Snapshot here
})
But this is not working. How should I approach this? Thanks in advance for the help.
We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .
The character \uf8ff used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText .
If the user is authenticating and exists, you will know their uid so you can read in their node from /users. There is a Firebase web view for your data, it's called the Firebase Dashboard and can be accessed once you log into your account on the Firebase website.
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)
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With