0

There are two ways of fetching data from server I used:

1)

firebaseService.databaseReference
            .child("followers/\(id)")
            .queryOrdered(byChild: "name")
            .observeSingleEvent(of: .value)

2)

firebaseService.databaseReference
            .child("followers")
            .child(id)
            .queryOrdered(byChild: "name")
            .observeSingleEvent(of: .value)

The structure is: Firebase database structure

id is a string of userID. Why with first time I get snapshot with key id and value null and in the second time I get snapshot with key id and value array of snapshots?

The second time is the value i need. So the first time don't work as expected.

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
  • Can you confirm that by the time the first query is sent `id` isn't empty? – eshirima Nov 14 '17 at 16:31
  • Isn't. I debugged it. – Nike Kov Nov 14 '17 at 16:32
  • why don't you use `.child("followers/"+id)` – Hareesh Nov 14 '17 at 16:33
  • Try appending to the next path inside the `id`. So your query becomes `.child("followers/\(id)/")` – eshirima Nov 14 '17 at 16:37
  • It's no matter how to write the path with slash. The question is about another thing. – Nike Kov Nov 14 '17 at 16:37
  • 2
    If these queries are both pointing to the same place, being executed on the same data and at the same time, they should return the same results. When they return a [`DataSnapshot`](https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DataSnapshot), do they both have the same `.`[`ref`](https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DataSnapshot#ref)`.`[`url`](https://firebase.google.com/docs/reference/swift/firebasedatabase/api/reference/Classes/DatabaseReference#url)? – Grimthorr Nov 14 '17 at 16:56
  • 1
    The two snippets do the same. If you're seeing difference in the results, it's not because of the snippets themselves but due to something else. One thing that immediately comes to mind is the order in which you execute the snippets: swap them around, and I suspect the "other one" will give you the result you need. If that is the case, I suspect you're using disk persistence. Disk persistence and `observeSingleEventOf` don't mix well. See https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent – Frank van Puffelen Nov 14 '17 at 17:41
  • After some seeking, i believe that it really could be a bug with `offline persistance` https://firebase.google.com/docs/database/ios/offline-capabilities. Because after a while i tried again this two ways and both work like expected. – Nike Kov Nov 14 '17 at 21:57

0 Answers0