Access data stored as an object in the Firestore database

database structure

I recently started testing a new Firebase db Firestore document for training, I am stuck now to access the value store inside the document as an object.

I use the code below to access the Privacy object stored in a document, but I'm not sure how to access Key - Value ? For example, I have 3 sub Key - Value pairs in an object, how can I access and edit it separately?

 DocumentReference docRef = FirebaseFirestore.getInstance().collection("Users").document("PQ8QUHno6QdPwM89DsVTItrHGWJ3"); docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document != null) { Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData().get("privacy")); Object meta_object = task.getResult().getData().get("privacy"); } else { Log.d(TAG, "No such document"); } } else { Log.d(TAG, "get failed with ", task.getException()); } } }); 

Any help is appreciated, thanks.

+5
source share
1 answer

The privacy field in your document can be considered Map<String, Boolean> , so you can specify the value of this field in such a variable:

 HashMap<String, Boolean> privacy = (HashMap<String, Boolean>) task.getResult().getData().get("privacy"); 

Now the main problem is that you will most likely see a compiler β€œuntested throw” warning, because casting a Map like this is not ideal, since you cannot guarantee that the database structure will always contain String : Boolean values in this field.

In this case, I would suggest using custom objects for storage and retrieve objects in your database that will automatically handle marching and casting for you:

 DocumentReference docRef = FirebaseFirestore.getInstance().collection("Users").document("PQ8QUHno6QdPwM89DsVTItrHGWJ3"); docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document != null) { User user = task.getResult().toObject(User.class); } else { Log.d(TAG, "No such document"); } } else { Log.d(TAG, "get failed with ", task.getException()); } } }); 

If your User class matters:

 public class User { private String username; private HashMap<String, Boolean> privacy; public User() {} public User(String username, HashMap<String, Boolean> privacy) { this.username = username; this.privacy = privacy; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public HashMap<String, Boolean> getPrivacy() { return username; } public void setPrivacy(HashMap<String, Boolean> privacy) { this.privacy = privacy; } } 

In this example, calling User user = task.getResult().toObject(User.class) will march the entire document into an instance of your User object, and then you can access the privacy map with:

 HashMap<String, Boolean> userPrivacy = user.getPrivacy(); 

Each field in the document will be mapped to a field with the same name inside your custom object, so you can also add settings or photo_url in the same way. You just need to remember:

Each custom class must have an open constructor that takes no arguments. In addition, the class must include a public getter for each property.

+5
source

Source: https://habr.com/ru/post/1272818/


All Articles