Search code examples
javaandroidfirebasefirebase-realtime-databasefirebaseui

Android - FirebaseRecyclerOptions recursive in the the db


I'm a little stuck, to create FirebaseRecyclerOptions that should present the results in recursive in database I mean if I had in Data base (Crossfit - > APR_09_2021 -> 10_12 -> "some data collection") So I noticed if I didn't pass the specific path to "some data collection" so it can't retrieve the data I mean: DatabaseReference mbase = FirebaseDatabase.getInstance().getReference().child("Crossfit").child(APR_09_2021) So my question how do I get all results from the database. for all the dates that I have under the CrossFit Thanks.

    DatabaseReference mbase =  FirebaseDatabase.getInstance().getReference().child("Crossfit");
    recyclerView = findViewById(R.id.recycler_plan);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    FirebaseRecyclerOptions<TrainingTrainerCollector> options = new FirebaseRecyclerOptions.Builder<TrainingTrainerCollector>()
            .setQuery(mbase, TrainingTrainerCollector.class).build();
    recycleViewAdapter = new TrainerRecyclerviewViewAdapter(options,"Crossfit");
    recyclerView.setAdapter(recycleViewAdapter);
    recycleViewAdapter.startListening();
    super.onStart();

https://i.sstatic.net/x4Fik.png https://i.sstatic.net/1CW2U.png


Solution

  • You cannot do that with your actual database structure. When you are using the following reference:

    DatabaseReference mbase =  FirebaseDatabase.getInstance().getReference().child("Crossfit");
    

    It means that you are trying to read all children under "Crossfit". So when using the Firebase-UI library, it means that you can only get children one level deep. According to your schema, your data is two-level deep, see?

    APR_10_2021 -> 10_12 -> Data
    

    There are two ways in which you can solve this. The first option you have is to create a new node called "allCrossfits" where you should add all "TrainingTrainerCollector" objects. This practice is called denormalization and is a common practice when it comes to Firebase. If you are new to NoSQL databases, I recommend you see this video, Denormalization is normal with the Firebase Database for a better understanding.

    So, your new reference should look like this:

    DatabaseReference mbase =  FirebaseDatabase.getInstance().getReference().child("allCrossfits");
    

    This reference should be passed to the "options" object.

    The second option that you have is to remove the "time" level from your database tree, as it is already present in your "time" field. So new your schema should look like this:

    Firebase-root
      |
      --- Crossfit
            |
            --- APR_10_2021
            |    |
            |    --- Current_Participant: "0"
            |    |
            |    --- Date: "APR 10 2021"
            |    |
            |    --- Max_Participant: "5"
            |    |
            |    --- Time: "10:00-12:00"
            |
            --- APR_14_2021
                 |
                 --- //Data
    

    In this case, you only need to change the way you are adding the data to the database. Your actual "mbase" reference will perfectly fine.