'is there best way to read firedatabase for recycleview in multiple child nodes?

Hi i have this database: farmerUser, user, and livelihood.

image of my nodes in firebase

Each nodes contains information in separate nodes.

For example: i want a farmer with this specific job and gets the contact information in user node and get its job description in this another node.

This is my code. This code is working, but i wonder if there is a best way to read multiple nodes to store it to my recycleview.

investingRecycleView.setLayoutManager(new LinearLayoutManager(this.getContext()));
FirebaseDatabase.getInstance().getReference().child("farmerUser").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot1 : dataSnapshot.getChildren()) {
            String id = postSnapshot1.getKey();
            String farmMethod = postSnapshot1.child("farmingMethod").getValue().toString();
            FirebaseDatabase.getInstance().getReference().child("ventureHeader").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot4) {
                    for (DataSnapshot postSnapshot3 : snapshot4.getChildren()) {
                        String expectedReturn = postSnapshot3.child("expectedReturn").getValue().toString();
                        FirebaseDatabase.getInstance().getReference().child("livelihood").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot1) {
                                for (DataSnapshot postSnapshot2 : snapshot1.getChildren()) {
                                    String budget = postSnapshot2.child("budget").getValue().toString();
                                    String farmTag = postSnapshot2.child("farmingTag").getValue().toString();
                                    FirebaseDatabase.getInstance().getReference().child("user").orderByKey().equalTo(id).addValueEventListener(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(@NonNull DataSnapshot snapshot3) {
                                            for (DataSnapshot postSnapshot3 : snapshot3.getChildren()) {
                                                String name = postSnapshot3.child("firstName").getValue().toString();
                                                String lastName = postSnapshot3.child("lastName").getValue().toString();
                                                String phoneNum = postSnapshot3.child("phoneNum").getValue().toString();
                                                FarmerVentureListModel farmerVentureListModel = new FarmerVentureListModel("Name:"+name + " " + lastName, "Farming Method:"+farmMethod,"#"+ phoneNum, "Farming tags:"+farmTag, "Budget:"+budget,"Expected return:"+expectedReturn);
                                                list.add(farmerVentureListModel);
                                                investingRecycleView.setAdapter(farmerVentureInvestingRow);
                                            }
                                        }
                                        @Override
                                        public void onCancelled(@NonNull DatabaseError error) {
                                        }
                                    });
                                }
                            }
                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

I wonder if there is a way like join each nodes without reading the entire data to find the match



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source