android - Firebase attaching listener to a Query object -


according firebase documentation it's bad practice attach listener high level in database hierarchy. have high level field called "places" (very large) , i'm getting reference it. when i'm going attach listener (childeventlistener) i'm using

firebasedatabase database = firebasedatabase.getinstance(); databasereference placesreference = database.getreference("places"); placesreference.orderbychild("city").equalsto("madrid").addchildeventlistener(....) 

to retrieve places in city.

my question - if listener attached places

city = "madrid" 

or attached entire places level hierarchy , therefore bad practice.

edit

this database

[...] "places" : {   "someplaceid_1" : {       "address" : "some address",       "city" : "madrid",       "coord_latitude" : 12.122121,       "coord_longitude" : 12.122121   },   "someplaceid_2" : {       "address" : "some address",       "city" : "madrid",       "coord_latitude" : 12.122121,       "coord_longitude" : 12.122121   },   "someplaceid_3" : {       "address" : "some address",       "city" : "barcelona",       "coord_latitude" : 12.122121,       "coord_longitude" : 12.122121   },   [...] }, [...] 

so, attached places or places city : "madrid"?

if using placesreference.orderby("city").equalsto("madrid") means reference set on placesreference. if have in database 1 city named madrid, don't need use orderby() method, need use line of code:

placesreference.child("city").child("madrid").addchildeventlistener(....) 

which correct because 2 steps down in hierarchical tree. in case, getting data madrid node.

edit:

seeing database after edit, can way in have added listener correct. way in can achieve using current database structure. remember, listener attached on exact reference put it. in case attached places (someplaceid_1, someplaceid_2 , on), not @ particular have child have value of "madrid". using .orderbychild("city").equalsto("madrid") order. using order equalsto("madrid") on value, not mean listener set there. in example explained above, listener put 2 steps down in three.

the solution in case flatten database. means need add node in can add places madrid. add listener on particular node.

i recomand reading post, structuring firebase data correctly complex app better understanding.

hope helps.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -