java - Filtering by grandchild property in firebase [in android] -


context :

hi, i'm working on android project backed firebase.

i have set denormalized data structure relates polls users (many-to-many relationship) via way of votes. here image displaying votes path of database. structure follows :

votes -> [polluid] -> [votepushuid] -> vote object

enter image description here

so in example have single poll has 4 votes.

i want run check see if user has voted on poll. in order fetch pollsuid, run through votes see if of them contain voteruid property == user uid.

this done follows :

firebasehandler.getinstance().getmaindatabaseref()                         .child(firebaseconstants.votes) //votes root                         .child(pollkey) //polluid                         .orderbychild("voteruid")                         .equalto(firebasehandler.getinstance().getuseruid())                         .limittofirst(1)                         .addlistenerforsinglevalueevent(new valueeventlistener() {                             @override                             public void ondatachange(datasnapshot datasnapshot) {                                if(!datasnapshot.exists()) { 

if datasnaptshot exists know user has voted on poll , can handle in java logic.

problem :

the datasnapshot received ondatachange null (ie not exist) when searching specific user's vote on specific poll. know fact vote exists in db through inspecting data, , useruid correct via debugging. removing equalto , limittofirst returns of votes poll without problem stem of ref correct. implies me issue created 1 of 2 methods mentioned. stranger fact approach work @ times, not @ others.

question : how return list of firebase stored objects filtered grandchild property? if not possible more appropriate datastructure problem?

on further note i've seen people taking approach of using query instead of databasereferences. perhaps might have current issue.

your query correct. have no problem running query using own db. it's userid doesn't match. databasereference extends query, that's why can access query's methods.

a database structure alternative be

{ "users_votes": {         "<userid>": {             "<pollid1>" : true,             "<pollid2>" : true,             "<pollid3>" : true         }     } } 

set value node once user voted poll.

to check if user has voted poll

usersvotesref.child(firebasehandler.getinstance().getuseruid())     .child(pollkey).addlistenerforsinglevalueevent(valueeventlistener); 

Comments