删除Parse上的多个对象

时间:2014-10-31 06:34:18

标签: java android parse-platform

我有一个半复杂的方法来删除Parse.com后端上的3种不同类型的ParseObjects。这是代码块:

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());
                    }
                }
            });
        }
    });

现在,这背后的逻辑是,给定一个测验对象ID,它运行一个查询并获得与该测验相关的问题。然后,在找到问题列表后,对于列表中的每个问题,它会运行查询以查找其相关答案。对于列表中的每个答案,它将删除答案对象,然后删除问题对象。对返回问题列表中的每个问题完成后,它会删除所有问题都与之相关的测验对象,并通过调用getQuizzes();来刷新列表来结束。

我已确认测验objectId正确传递。

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!

如您所见,不会调用findAnswers的Log语句。它也没有调用“findQuestions Query done!”日志声明。所以这个方法的执行让我搞砸了。问题和答案都没有被删除。

有什么想法吗?

答案:我在Parse的后端的Category类中调用了问题查询,而不是Questions类。糟糕!

0 个答案:

没有答案