Home > Net >  Read data from Firebase database when I have multiple unique ids
Read data from Firebase database when I have multiple unique ids

Time:04-06

I'm having trouble writing in my database because I have multiple unique ids that I don't know how to access. I know that for writing I need to use something like this:

DatabaseReference ref=database.getReference().child("Children")
ChildData data = new ChildData(fullName,age);
ref.push().setValue(data);

My database looks like this:

  {
  "Children" : {
    "55hxObZjRLY9PSZxZxGgSTRKzpc2" : {
      "-MzUih5e40OsWPF_Dj0q" : {
        "age" : "22",
        "fullName" : "Cristina "
      },
      "plays" : {
        "-MznnNih5fItYf2usXB4" : {
          "centiseconds" : "70",
          "currentDate" : "01.04.2022",
          "currentHour" : "04:23:30",
          "numberOfFails" : "5",
          "seconds" : "2"
        }
      }
    }
  },
  "Data" : {
    "id1" : {
      "centiseconds" : "70",
      "currentDate" : "01.04.2022",
      "currentHour" : "04:23:30",
      "numberOfFails" : "5",
      "seconds" : "2"
    }
  }
}

I need the "plays" child to be under "fullName" in "-MzUih5e40OsWPF_Dj0q", not as a separated child. How can I access the path without hardcoding it like this ref.child("MzUih5e40OsWPF_Dj0q").child("plays").push().setValue(data)? I will have multiple children in "Children" with different ids, I won't be able to harcode them.

Is there any function that returns the path for the unique id?

Here is the entire function:

public void writeNewData
            (String fullName,String age) {
    DatabaseReference reference = database.getReference().child("Data");         
    DatabaseReference ref=myRef.push();
    ChildData data = new ChildData(fullName,age);
    ref.push().setValue(data);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {
                DatabaseReference playRef=ref.child("plays");
                DatabaseData databaseData = ds.getValue(DatabaseData.class);
                playRef.push().setValue(databaseData);
            }
        }

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

Edit: This is my desired schema:

{
  "Children" : {
    "55hxObZjRLY9PSZxZxGgSTRKzpc2" : {
      "-MzUih5e40OsWPF_Dj0q" : {
        "age" : "22",
        "fullName" : "Cristina ",
        "plays" : {
          "-MznnNih5fItYf2usXB4" : {
            "centiseconds" : "70",
            "currentDate" : "01.04.2022",
            "currentHour" : "04:23:30",
            "numberOfFails" : "5",
            "seconds" : "2"
          }
        }
      },
    }
  },
  "Data" : {
    "id1" : {
      "centiseconds" : "70",
      "currentDate" : "01.04.2022",
      "currentHour" : "04:23:30",
      "numberOfFails" : "5",
      "seconds" : "2"
    }
  }
}

CodePudding user response:

If you want the plays information to be a sibling of the age and fullName properties, then you will need to have a plays field/property in your ChildData class.

You'll need two classes, one to represent a "play" and one to represent a "child". At its minimum these will look like:

public class Play {
  public String centiseconds, currentDate, currentHour, numberOfFails, seconds;
}

public class Child {
  public String age, fullName;
  public Map<String, Play> plays = new HashMap<>();
}

If you read/write this class, you will get plays nested under the child's data.

  • Related