Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Deleting multiple ParseObjects

I've got a semi-complex method to delete 3 different kinds of ParseObjects on my Parse.com backend. Here's the block of code:

ParseQuery<ParseObject> findQuestions = new ParseQuery<ParseObject>(ParseConstants.CLASS_QUESTIONS);
    findQuestions.whereEqualTo(ParseConstants.KEY_QUESTIONS_BELONGS_TO, quiz.getObjectId());
Log.d("KMH", "findQuestions Query starting for quiz: " + quiz.getObjectId());
    findQuestions.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> questions, ParseException e) {
            if (e == null) {
                Log.d("KMH", "findQuestions Query returned...");
                for (ParseObject question : questions) {
                    ParseQuery<ParseObject> findAnswers = new ParseQuery<ParseObject>(ParseConstants.CLASS_ANSWERS);
                    findAnswers.whereEqualTo(ParseConstants.KEY_ANSWER_BELONGS_TO, question.getObjectId());
                    Log.d("KMH", "findAnswers Query starting for question: " + question.getObjectId());
                    findAnswers.findInBackground(new FindCallback<ParseObject>() {
                        @Override
                        public void done(List<ParseObject> answers, ParseException e) {
                            if (e == null) {
                                Log.d("KMH", "findAnswers Query returned...");
                                for (ParseObject answer : answers) {
                                    answer.deleteInBackground();
                                }
                                Log.d("KMH", "findAnswers Query done!");
                            } else {
                                Log.d("KMH", "Parse error: " + e.getMessage());
                            }
                        }
                    });
                    question.deleteInBackground();
                }
                Log.d("KMH", "findQuestions Query done!");
            } else {
                Log.d("KMH", "Parse error: " + e.getMessage());
            }
            quiz.deleteInBackground(new DeleteCallback() {
                @Override
                public void done(ParseException e) {
                    if (e == null) {
                        Log.d("KMH", "Quiz deleted!");
                        getQuizzes();
                    } else {
                        Log.d("KMH", "Parse error: " + e.getMessage());
                    }
                }
            });
        }
    });

Now, the logic behind this is that, given a quiz object ID, it runs a query and gets the questions related to that quiz. And then, after finding that List of questions, for each question in the list, it runs a query to find it's associated answers. For each answer in the list, it deletes the answer object, and then deletes the question object. Once that's done for every question in the list of returned questions, it deletes the quiz object that all the questions are related to, and ends by calling getQuizzes(); to refresh the list.

I have confirmed the quiz objectId is being passed correctly.

10-31 02:25:41.901  29950-29950/me.kevinhaube.quizme D/KMH﹕ findQUestions Query starting for zR7GaOtwgk
10-31 02:25:42.061  29950-29950/me.kevinhaube.quizme D/KMH﹕ findQUestions Query returned...
10-31 02:25:42.221  29950-29950/me.kevinhaube.quizme D/KMH﹕ Quiz deleted!

As you can see, the Log statements for findAnswers don't get called. It also doesn't call the "findQuestions Query done!" Log statement. So it's something in the execution of this method that is messing me up. Neither the questions, nor the answers are being deleted.

Any ideas?

EDIT: Confirmed that the ParseQuery on the questions containing the "belongsToQuiz" value of the quiz's objectId returns nothing! The confusing part is why. I've trippled checked the ParseConstant key, and it's the correct spelling and everything. quiz.getObjectId() returns the correct id, and there are questions with that key associated to it!

ANSWER: Turns out I was calling the question query in the Category class, instead of the Questions class on my back-end. Silly me!

10-31 03:26:29.242  15851-15851/me.kevinhaube.quizme D/KMH﹕ findQuestions Query starting for quiz: YbDgpKERpv
10-31 03:26:29.592  15851-15851/me.kevinhaube.quizme D/KMH﹕ findQuestions Query returned...
10-31 03:26:29.592  15851-15851/me.kevinhaube.quizme D/KMH﹕ [com.parse.ParseObject@42d98180]
10-31 03:26:29.592  15851-15851/me.kevinhaube.quizme D/KMH﹕ Question Qg1DVJQ9MD
10-31 03:26:29.592  15851-15851/me.kevinhaube.quizme D/KMH﹕ findAnswers Query starting for question: Qg1DVJQ9MD
10-31 03:26:29.602  15851-15851/me.kevinhaube.quizme D/KMH﹕ findQuestions Query done!
10-31 03:26:29.752  15851-15851/me.kevinhaube.quizme D/KMH﹕ Quiz deleted!
10-31 03:26:29.772  15851-15851/me.kevinhaube.quizme D/KMH﹕ findAnswers Query returned...
10-31 03:26:29.792  15851-15851/me.kevinhaube.quizme D/KMH﹕ findAnswers Query done!

Finished. :)