Is it possible to add an Object inside a Child (Obj) in firebase?

0

I am currently starting to use firebase and I want to implement a database in my Android app, I am trying to upload two Objects, for now I have uploaded a Person type Object in the following way:

in OnCreate :

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mDataBaseReference = . 
 mFirebaseDatabase.getReference().child("Personas");

Then to add my object to the database:

mClientesDataBaseReference.push().setValue(nuevaPersona);

Achieving upload this:

personas : {
 "dsgasdgjbnakjds" : {
      "Nombre" : "Lalo",
      "FotoId" : "1",
      "edad" : "18",

      } 
 }

My object is something like this:

     public Cliente(){
         //Variables de tipo String
         nombre = "";
         FotoId = "";
         Edad = "";
         // Variable tipo Mascota
         mastoca = new Mascota();
    }

    public Mascota{
      //String
      Nombre = "";
      Edad= "";
    }

However, even declaring the pet as part of the Persona object, it does not appear in the database.

I would like to be able to add the Object ( mascota ) within this same Object of persona and achieve something similar to this:

personas : {
 "lksdjnflkasdg" : {
      "Nombre" : "Lalo",
      "FotoId" : "1",
      "edad" : "18",
      "Mascota" : {
           "nombreMascota" : "LittleDog",
           "edadMascota" : 3
      } 
 }, 
 "lkjndslfknlksdn" : {
      "Nombre" : "Roberto",
      "FotoId" : "2",
      "edad" : "20",
      "Mascota" : {
           "nombreMascota" : "LittleDog",
           "edadMascota" : 5
      } 
}

Thanks

    
asked by Alex Trujillo 25.06.2017 в 08:53
source

1 answer

1

The object should persist if it complies with the rules required by Firebase. 1) Have an empty constructor 2) Have public getter for each property that you want to persist

If the parent object as well as the embedded objects comply with the rules, they will persist (and be able to recover later from the snapshot) automatically.

    
answered by 27.06.2017 / 20:00
source