Get all sub items that contain user id in Firebase Database

时间:2017-12-18 06:08:33

标签: android firebase firebase-realtime-database

I am trying to show all friend requests of the current user by searching through the firebase db and matching on the user id under the receiver node but keep returning nothing.How do i filter by user id in the receiver node ?

    public void searchForPendingFriendRequests(){
    mUserFriendsReference = mFirebaseDatabase.getReference("friend_request/receiver" + FirebaseAuth.getInstance().getCurrentUser().getUid());
    mUserFriendsReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.getChildrenCount() != 0) {
                mPendingFriendsCard.setVisibility(View.VISIBLE);
                mAllRequestsText.setVisibility(View.VISIBLE);

                for(DataSnapshot child: dataSnapshot.getChildren()) {
                    FriendRequest friendRequest = child.getValue(FriendRequest.class);
                    friendToAdd = friendRequest.getSender();
                    populateCurrentAddFriendData(friendRequest);
                    break;
                }
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}

db looks like this friend_request ---> {pushid} ---> receiver ---> userid

enter image description here

1 个答案:

答案 0 :(得分:2)

I think you forgot to add this from multiprocessing import Process, Queue from concurrent.futures import _base import threading from time import sleep def foo(x,q): print('result {}'.format(x*x)) result = x*x sleep(5) q.put(result) class MyProcess(Process): def __init__(self, target, args): super().__init__() self.target = target self.args = args self.f = _base.Future() def run(self): q = Queue() worker_thread = threading.Thread(target=self.target, args=(self.args+ (q,))) worker_thread.start() r = q.get(block=True) print('setting result {}'.format(r)) self.f.set_result(result=r) print('done setting result') def start(self): f = _base.Future() run_thread = threading.Thread(target=self.run) run_thread.start() return f def cb(future): print('received result in callback {}'.format(future)) def main(): p1 = MyProcess(target=foo, args=(2,)) f = p1.start() f.add_done_callback(fn=cb) sleep(10) if __name__ == '__main__': main() print('Main thread dying') in the mUserFriendsReference
Change this line

"/"

to this

  mUserFriendsReference = mFirebaseDatabase.getReference("friend_request/receiver" +FirebaseAuth.getInstance().getCurrentUser().getUid());