How to filter Firebase data in Swift?

前端 未结 3 829
灰色年华
灰色年华 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:05

    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)
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 06:27
    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)
        }
    })
    
    0 讨论(0)
提交回复
热议问题