Yes, it is possible to get the identifier without saving it using DocumentSnapshot
. I will try to give complete examples here.
I created a generic model class for storing id:
@IgnoreExtraProperties
public class Model {
@Exclude
public String id;
public <T extends Model> T withId(@NonNull final String id) {
this.id = id;
return (T) this;
}
}
Then you expand it using any model, you do not need to implement anything:
public class Client extends Model
, , age == 20
:
clients.whereEqualTo("age", 20)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()) {
Client client = document.toObject(client.class).withId(document.getId());
}
} else {
}
}
});
EventListener
, , :
clients.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange change : documentSnapshots.getDocumentChanges()) {
Client client = document.toObject(client.class).withId(document.getId());
}
}
});
documentSnapshot.getId())
.
@IgnoreExtraProperties