Reputation: 7895
I have a Class called Routine, it has the attributes name and exercises. Exercises in an ArrayList of type RoutineExercises.
When I write my Routine to my Firestore Database, it works like it should and adds a document with name and an array for the exercises with all the Objects inside my ArrayList.
However I think its propably not a good idea to store the exercises in the same document as the name, because I sometimes don't need these Exercises. So I wanted to create another Collection "RoutineExercises" inside my Routine Document which contains the ArrayList of RoutineExercises. This is what my Code looks like:
fm.getColRefUserRoutines().add(routine).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
documentReference.collection("RoutineExercises").add(routine.getExcersises()).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d("Success", "!!");
}
});
}
});
while fm.getColRefUserRoutine()
return my Collection of Routines.
But I the Exception:
java.lang.IllegalArgumentException: Invalid data. Data must be a Map or a suitable POJO object
Upvotes: 2
Views: 1906
Reputation: 462
From what I understood from your question you want to create a subcollection of exercises inside your Routine Document. The following code will add all the exercises to the RoutineExercises collection inside of your Routine Document. As far as my knowledge goes you can not directly save an ArrayList as a subcollection.
fm.getColRefUserRoutines().add(routine).addOnSuccessListener(new
OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
for (Exercise exercise: routine.getExcersises()) {
documentReference.collection("RoutineExercises").add(exercise).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d("Success", "!!");
}
});
}
}
});
Upvotes: 0
Reputation: 462
In order to use add() you must either pass a Map or an Object. In case of adding an object you must have an empty constructor and all the getters for its atributehere .
Each custom class must have a public constructor that takes no arguments. In addition, the class must include a public getter for each property
If you want to insert your list of exercises you could try this:
HashMap<String, Exercise> exerciseMap = new HashMap<String, Exercise ();
for (Exercise exercise : exerciseList) {
exerciseMap.put(exercse.getExerciseCode(), exercise);
}
Base on this answer.
Upvotes: 2