How to filter Firebase data in Swift?

You have a few small mistakes in there. Overall nothing too bad, but combined they'll never work:

  1. calling any of the query... methods returns a new object
  2. you need to orderByChild() before you can filter on its value
  3. you need to loop over the results

Combining 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)
    }
})