0

How can I get all data in a particular node that starts with a given String.

For example, let's say I want to return all tags that start with the String "bas" (it should return [basketball: 1, baseball: 4].

tags: {
    basketball: 1,
    baseball: 4,
    football: 0
}

I thought something like this (following the answer in this SO question for reference), but unfortunately it doesn't return anything...

ref.child("tags")
     .queryStarting(atValue: text)
     .queryEnding(atValue: "\(text)zzzzzzz")
     .getData { error, snap in
        // ...
}
Eric
  • 569
  • 4
  • 21
  • 1
    I'm wondering if it's trying to order/filter on priority here. Can you try adding `queryOrderedByKey()` to the query to see if that makes a difference? --- If that doesn't change anything can you try with only hard-coded values and (if it still occurs) edit the code to use those values? It'd also be good to see how you handle the result, especially checking for any errors. – Frank van Puffelen Jun 28 '22 at 19:43
  • @FrankvanPuffelen You were right, I just had to add `queryOrderedByKey()`. Thanks – Eric Jun 30 '22 at 07:49

1 Answers1

1

Since you don't specify what you want to order the child nodes on, the server might (I'm not entirely sure anymore) order them on their priority value and then try to filter on that. Either way, it is hard to reason about implicit behavior, so I recommend always specifying the sort order, which in this case would be:

ref.child("tags")
     .queryOrderedByKey()
     .queryStarting(atValue: text)
     .queryEnding(atValue: "\(text)zzzzzzz")
     .getData { error, snap in
        // ...
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807