0

I am inquiring if it is possible to generate a model class from a Firestore document snapshot or retrieve and bind field values to views from a document snapshot without having a model class or with a model class that has fewer objects compared to the fields in the Firestore document.

For example in this code got from the documentation:

    public static class Post {
    public String author;
    public String title;

    public Post(String author, String title) {
    // ...
    }

 }
    // Get a reference to our posts
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");

    // Attach a listener to read the data at our posts reference
    ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    Post post = dataSnapshot.getValue(Post.class);
    System.out.println(post);
 }

We expect the document to have 2 fields. But suppose it has more than two fields that were not declared in the model class. How would one retrieve the values of those other fields and bind them to views in Android?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

First of all, you talking about Firestore but you shared code related to Firebase real-time database. If it's a mistake, that's the response about Firestore.

am inquiring if it is possible to generate a model class from a Firestore document snapshot

There is no way you can simply create a Java class according to the fields you have in the database. There are some libraries that can help you achieve that but that's not the point.

retrieve and bind field values to views from a document snapshot without having a model class

This is certainly possible. The DocumentSnapshot class contains many flavors for the get() method starting with get(String field) that can help get the value of a field and according to its type you can cast it as needed and ending with specialized methods like getLong(String field), getBoolean(String field) or even getData() that:

Returns the fields of the document as a Map or null if the document doesn't exist.

So you can get data from Firestore without having a model class.

or with a model class that has fewer objects compared to the fields in the Firestore document

Yes, you can have a model class that has fewer fields compared to what you have in the database base but what's the purpose? Why would you store some data in the database that you don't want to get it? Usually, we only store data that we use and we do this way to save bandwidth and resources.

we expect the document to have 2 fields. but suppose it has more than two fields that were not declared in the model class. how would one retrieve the values of those other fields and bind them to views in android

Let's assume that besides the author and title properties you have in the database another one named description. Since this property does not exist in your Post class, the following lines of code will produce a compilation error:

For Firestore:

Post post = document.toObject(Post.class);
String description = post.description;

For Firebase real-time database:

Post post = dataSnapshot.getValue(Post.class);
String description = post.description;

To be able to get the value of that property you should use the following line of code:

For Firestore:

String description = document.getString("description");

For Firebase real-time database:

String description = dataSnapshot.child("description").getValue(String.class);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Sorry for the late reply I dont always have access to internet. But Thank you, the answer is well explained. and it worked perfectly – maltez kolt Nov 21 '19 at 04:36
  • By the way the question was firestore, but i will also make good use on the info regarding real time database – maltez kolt Nov 21 '19 at 04:40