无法使用ParseQuery的结果 [英] Unable to use the result of a ParseQuery

查看:327
本文介绍了无法使用ParseQuery的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的解析和我做一个查询获取表。
正如你可以在下面的code看到,该列表LOCALparseQuestionList期间正确填充的findInBackground内循环。一旦完成,该LOCALparseQuestionList是空的(日志打印0的大小,我看到与使用调试器时)。
我应该如何获取正确的数据,并填充我LOCALparseQuestionList?

 公开名单< QuestionStruct> getParseAllQuestions(){
    最终名单< QuestionStruct> LOCALparseQuestionList =新的ArrayList< QuestionStruct>();
    //选择所有查询
    ParseQuery<&的parseObject GT; questionQuery = ParseQuery.getQuery(triviaQuestions);
    questionQuery.findInBackground(新FindCallback<&的parseObject GT;(){
        公共无效完成(列表<&的parseObject GT; allQuestions,ParseException的E){
            如果(E == NULL){
                parseQuestionList = allQuestions;
                Log.d(TAG,取自+ allQuestions.size()+所有问题);
                对于(曲的parseObject:allQuestions){
                    QuestionStruct currentQuestion =新QuestionStruct();
                    currentQuestion.setID(qu.getInt(ID));
                    currentQuestion.setQuestion(qu.getString(问题));
                    currentQuestion.setCorrectAnswer(qu.getString(正确));
                    currentQuestion.setPossibleAnswer(qu.getString(wrong_1));
                    currentQuestion.setPossibleAnswer(qu.getString(wrong_2));
                    currentQuestion.setPossibleAnswer(qu.getString(wrong_3));
                    currentQuestion.setPossibleAnswer(qu.getString(正确));
                    LOCALparseQuestionList.add(currentQuestion);
                    Log.d(TAG,取自+ LOCALparseQuestionList.size()+LOCALparseQuestionList);
                }
            }其他{
                Log.d(TAG,错误:+ e.getMessage());
            }
        }
    });
    Log.d(TAG,questionList大小:+ LOCALparseQuestionList.size());    返回LOCALparseQuestionList;
}


解决方案

它是一个头号的误解有关异步功能:code中的查找功能查找功能后不运行下方。它收到运行。

在功能日志的最后一个日志语句,return语句返回一个空列表,因为列表后填充,查找完成后并返回结果。依赖于 LOCALparseQuestionList 正在填充任何你做必须查找的回调中完成。

I'm using Parse and I'm doing a query to fetch a table . As you can see in the code below, the list LOCALparseQuestionList is populated correctly during the for loop inside the findInBackground. Once it's done, the LOCALparseQuestionList is empty (the log prints 0 size and I see the same when using the debugger). How should I fetch correctly the data and populate my LOCALparseQuestionList?

 public List<QuestionStruct> getParseAllQuestions() {
    final List<QuestionStruct> LOCALparseQuestionList = new ArrayList<QuestionStruct>();
    // Select All Query
    ParseQuery<ParseObject> questionQuery = ParseQuery.getQuery("triviaQuestions");
    questionQuery.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> allQuestions, ParseException e) {
            if (e == null) {
                parseQuestionList = allQuestions;
                Log.d(TAG, "Retrieved " + allQuestions.size() + " All questions");
                for (ParseObject qu : allQuestions) {
                    QuestionStruct currentQuestion = new QuestionStruct();
                    currentQuestion.setID(qu.getInt("id"));
                    currentQuestion.setQuestion(qu.getString("question"));
                    currentQuestion.setCorrectAnswer(qu.getString("correct"));
                    currentQuestion.setPossibleAnswer(qu.getString("wrong_1"));
                    currentQuestion.setPossibleAnswer(qu.getString("wrong_2"));
                    currentQuestion.setPossibleAnswer(qu.getString("wrong_3"));
                    currentQuestion.setPossibleAnswer(qu.getString("correct"));
                    LOCALparseQuestionList.add(currentQuestion);
                    Log.d(TAG, "Retrieved  " + LOCALparseQuestionList.size() + " LOCALparseQuestionList ");
                }
            } else {
                Log.d(TAG, "Error: " + e.getMessage());
            }
        }
    });
    Log.d(TAG, "questionList size: " + LOCALparseQuestionList.size());

    return LOCALparseQuestionList;
}

解决方案

Its a the number one misunderstanding about asynchronous functions: the code underneath the find function does not run after the find function. It runs before it.

The last log statement in the function logs, and the return statement returns an empty list, because that list is populated later, after the find is done and the results are returned. Anything you do that depend on LOCALparseQuestionList being populated must be done within the find's callback.

这篇关于无法使用ParseQuery的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆