Persistence and reading of Firebase data

Asked

Viewed 556 times

1

would like to have a data structure in the Firebase database Realtime similar to this:

{

  "codigo": "01",

  "Data": "1234567890",

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

}

have the class:

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

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

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

If user was an Arraylist, how would this reading and writing?

2 answers

1

You can add a user with setValue() as follows: Using the data of 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 in firebase

1

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

  • Thanks, but when I have saved a user on some other screen, I want you to save all attributes. And putting annotation will block this. But the focus gives question is how to save and how to read and transform the answer into a query class object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.