Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore whereEqualTo, orderBy and limit(1) not working

I want to query my Workout Collection for the latest workout from a routine. Meaning I query with whereEqualTo my routineKey, order it by the Started TimeStamp in descending order and then limit to 1 and then take the this 1st Key/Id of the Workout.

However this does not work. whereEqualTo and orderBy work separately but not combined. What am I doing wrong?

fm.getColRefWorkout().whereEqualTo("routineKey", routineKey).orderBy("startTimeStamp", Query.Direction.DESCENDING).limit(1).get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { 
                @Override
                public void onSuccess(QuerySnapshot documentSnapshots) {
                    workoutKey = documentSnapshots.getDocuments().get(0).getId();
                    //To stuff with this workoutKey
                }
            });
like image 398
Jonas Avatar asked May 12 '18 10:05

Jonas


1 Answers

This query will not work unless you create an index for it. This can be done, by creating it manually in your Firebase Console or if you are using Android Studio, you'll find in your logcat a message that sounds like this:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...

You can simply click on that link or copy and paste the URL into a web browser and your index will be created automatically.

like image 159
Alex Mamo Avatar answered Oct 14 '22 11:10

Alex Mamo