Android Firestore convert array of document references to ListPojo
Yes it is. Please see the following lines of code:
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null) {
String uid = firebaseUser.getUid();
rootRef.collection("users").document(uid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
List<DocumentReference> list = (List<DocumentReference>) document.get("bookmarks");
List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (DocumentReference documentReference : list) {
Task<DocumentSnapshot> documentSnapshotTask = documentReference.get();
tasks.add(documentSnapshotTask);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list
for (Object object : list) {
TeacherPojo tp = ((DocumentSnapshot) object).toObject(TeacherPojo.class);
Log.d("TAG", tp.getFirstName());
}
}
});
}
}
}
});
}
So the List<Object> list
is actually the list that contains objects of type TeacherPojo
.
Firestore document object (documentSnapshot) to POJO with nested maps
As you already say voucher
is a Map
, so that's what you should use in your Java class too:
public class MyPOJO {
public String id;
public Map<String,Voucher> vouchers;
}
public class Voucher {
public String name, description;
}
Firebase Firestore : How to convert document object to a POJO on Android
With a DocumentSnapshot
you can do:
DocumentSnapshot document = future.get();
if (document.exists()) {
// convert document to POJO
NotifPojo notifPojo = document.toObject(NotifPojo.class);
}
How to map document Reference to POJO in firestore firebase for kotlin
You are getting the following error:
java.lang.RuntimeException: Could not deserialize object. Can't convert object of type com.google.firebase.firestore.DocumentReference to type com.firestorepoc.model.Biomarker
Because you have declared the content
property in your Biomarkers
class to be of type Biomarker
, while in the database is actually a DocumentReference
. So the exception is raised because there is no way in Kotlin in which you can cast an object of type DocumentReferenc
to an object of type Biomarker
.
To solve this, you have to change the content
property to be of type DocumentReference
as it is in your database.
Besides that, I see that the biomarkers
property is an array. If you need to map that array to a list of Biomarkers
objects (List<Biomarkers>
), please check out the following article:
- How to map an array of objects from Cloud Firestore to a List of objects?
Android Firestore querying particular value in Array of Objects
With your current document structure, it's not possible to perform the query you want. Firestore does not allow queries for individual fields of objects in list fields.
What you would have to do is create an additional field in your document that is queryable. For example, you could create a list field with only the list of string languages that are part of the document. With this, you could use an array-contains query to find the documents where a language is mentioned at least once.
For the document shown in your screenshot, you would have a list field called "languages" with values ["Swift", "Kotlin"].
Cast result of multiple tasks list to their POJO in Tasks.whenAllSuccess() (Firestore)
As I understand, you have two types of lists, userModelArrayList
which is of type UserModel, and likeModelArrayList
which is of type LikeModel. To be able to add each object in its corresponding type of list, you should create the lists of Task objects to be of type UserModel and LikeModel. So please change the following lines of code:
List<Task<DocumentSnapshot>> fetchUserTasks = new ArrayList<>();
List<Task<DocumentSnapshot>> fetchLikeTasks = new ArrayList<>();
to
List<Task<UserModel>> fetchUserTasks = new ArrayList<>();
List<Task<LikeModel>> fetchLikeTasks = new ArrayList<>();
So instead of adding the DocumentSnapshot
objects, add the actual objects. So use toObject()
before. That being said, you can then check each object type using instanceof
operator, like in the following lines of code:
Tasks.whenAllSuccess(combineUserTasks, combineLikeTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> objects) {
for (Object object : objects) {
if (object instanceof UserModel) {
userModelArrayList.add(userModel);
} else if (object instanceof LikeModel) {
likeModelArrayList.add(likeModel);
}
}
}
});
Now, you'll have each list populated with the corresponding object type.
Firestore query on collection
According to your last comment, I understand that you want to get all documents within a single collection and not to query multiple collections, which is not possbile for the moment in Firestore.
If you have a list of ids, then simply iterate over it and create for each id in the list the corresponding DocumentReference
and then add all those references to a List<DocumentReference>
. After that, iterate over the new list and for each reference create a Task
and then add all those Tasks objects to List<Task<DocumentSnapshot>>
.
In the end, just pass the list of Tasks to Tasks's whenAllSuccess() method:
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list
for (Object object : list) {
YourObject yb = ((DocumentSnapshot) object).toObject(YourObject.class);
Log.d("TAG", yb.getPropertyName);
}
}
});
In code it looks like my answer from this post:
- Android Firestore convert array of document references to List<Pojo>
Related Topics
Creating Runnable Jar with Gradle
Convert Unix Timestamp to Date in Java
Capture and Log the Response Body
Giving Jmenuitem's Name to It's Actionlistener
Mapstruct and Lombok Not Working Together
Why Doesn't Java Throw an Exception When Dividing by 0.0
Getting Strange Output When Printing Result of a String Comparison
Difference Between Java.Util.Random and Java.Security.Securerandom
Calculate Elapsed Time in Java/Groovy
How to Get the Methodinfo of a Java 8 Method Reference
Mapping Manytomany with Composite Primary Key and Annotation:
How to Make a Jtable Non-Editable
Read the Output from Java Exec
When Should We Use Observer and Observable