How to filter Firebase data in Swift?

前端 未结 3 843
灰色年华
灰色年华 2020-12-03 05:27

Basically, I have a structure called, topics which contains Title, Description and a Published flag (see screenshot below for clarific

3条回答
  •  隐瞒了意图╮
    2020-12-03 06:13

    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.

提交回复
热议问题