简体   繁体   中英

Swift Firebase Query - No Results in Snapshot

I am trying to query our LCList object by the "name" value, as shown in this image . The name key is just on the next level below the object. There are no additional levels to any of its other values.

The code I am using to do the query is: (Keeping in mind listsRef points to the LCLList object)

listsRef.queryOrderedByKey()
        .queryStarting(atValue: name)
        .observeSingleEvent(of: .value, with: { snapshot in

The snapshot from this query comes back with nothing in its value. I have tried ordering the results by the name value as well, with the same result. I have inspected the values returned with only the queryOrderedByKey() method call, and they match what is in the database. The issue is obviously with the .queryStarting(atValue:) method call.

I'm really puzzled by why this is not working as the same query pointed to our LCLUser object , with nearly the same structure, does get results. The two objects exist at the same level in the "Objects" directory seen in the previous image.

Any help at this point would be appreciated. I'm sure there's something simple I'm missing.

That query won't work for the Firebase structure shown in the question.

The query you have is as follows, I have added a comment on each line detailing what each line does

listsRef.queryOrderedByKey()   //<- examine the key of each child of lists
        .queryStarting(atValue: name)   //<- does that keys' value match the name var
        .observeSingleEvent(of: .value, with: { snapshot in   //do it once

and your current data looks like this

Objects
   LCLLists
      node_x
         creator: "asdasa"
         name: "TestList3"
      node_y
         creator: "fgdfgdfg"
         name: "TestList"

so what's happening here is the name var (a string) is being compared to the value of each key (a Dictionary) which cannot be equal. String ≠ Dictionary

    key                   value
["node_x": [creator: "asad", name: "TestList3"]

For that query to work, your structure would need be be like this

Objects
   LCLLists
      node_x: "TestList3"
      node_y: "TestList"

My guess is you want to stick with your current structure so, suppose we want to query for all names that are 'TestList' using your structure

let ref = self.ref.child("LCLLists") //self.ref is a class var that references my Firebase
let query = ref.queryOrdered(byChild: "name").queryEqual(toValue: "TestList")
query.observeSingleEvent(of: .value, with: { snapshot in
    print(snapshot)
})

and the result is a printout of node_y

      node_y
         creator: "fgdfgdfg"
         name: "TestList"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM