2

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jonas
  • 7,089
  • 15
  • 49
  • 110

2 Answers2

2

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.

Sila Siebert
  • 422
  • 4
  • 10
  • But in this case I don't have an array in my database. This way I won´t get it as an ArrayList when I retrieve my Object... – Jonas Apr 15 '18 at 12:52
  • Correct. You will get a Map, which you can then "transform" into an ArrayList by basically doing the opposite of what you did when saving it. – Sila Siebert Apr 15 '18 at 13:30
  • Yes but is it not possible to save the ArrayList as a Array in a separate Object? When I post my Routine Object I have an Document with an Array and a String with name. When I retrieve this Object It already fills in my ArrayList from this Array. Now I just want to split this Object, so I have this array not in the same Document as my name property. – Jonas Apr 15 '18 at 13:38
0

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", "!!");
                }
            });
        }

    }
});
Sila Siebert
  • 422
  • 4
  • 10