Persistence and readability of Firebase data

1

I would like to have a data structure in the Firebase realtime database look like this:

{

  "codigo": "01",

  "Data": "1234567890",

  "usuario": "{codigo : 01, nome : Rafael}"

}

I have the class:

class Consulta {
   String codigo;
   Long data;
   Usuario usuario;
}

The user class has more attributes, but I just want to save the ones that are in JSON.

How do I save these values to stay in this structure? How do I read these values?

If you were an Arraylist, how would that read and write?

    
asked by anonymous 07.02.2017 / 18:01

2 answers

1

To save that way, you'll have to have an object that contains the values code, date, and user. The user will be an object that should be transformed into JSON (string) before being sent to the firebase. But I do not recommend doing this because if you send the object with these two attributes + the user object, firebase takes care of creating the data tree in json alone. To exclude attributes from being saved, use the @Exclude annotation on top of each attribute you want not to save on the firebase.

    
07.02.2017 / 18:09
1

You can add a user with setValue () as follows: Using the data from your class above, it would look like this:

private void novaConsulta(int codigo, DateTime data, Usuario usuario) {
    Usuario usuario= Usuario User(codigo, nome);

    mDatabase.child("usuarios").child(codigo).setValue(usuario);
}

Assuming you already have this query structure and users on firebase

    
13.04.2017 / 05:31