0

In my endWorkout.java file, I am saving data into my Parse database using the following logic:

// Parse Storage
        ParseObject testObject = new ParseObject("TestOne");
        testObject.put("Device", ParseInstallation.getCurrentInstallation());
        testObject.put("Reps", inputList);
        testObject.saveInBackground();

Where I am first storing my Device ID for authentication purposes, and then storing inputList which is an ArrayList of integers.

In my Parse database, the data is properly saved, as shown below:

enter image description here

Now in my MainActivity.java, I would like to retrieve all the data in the Reps field of the Parse database for a single device. For example, the device yhmrKgokfS has 6 Arrays in the Parse database, I would like to sequentially retrieve each of them to display in a ListView on the screen.

Here is the logic I am trying to use:

List<ParseObject> importList = new ArrayList<ParseObject>();

//parse import list
        ParseQuery<ParseObject> query = ParseQuery.getQuery("TestOne");
        query.whereEqualTo("Device", ParseInstallation.getCurrentInstallation());
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> repList, ParseException e) {
                if (e == null) {
                    Log.d("Reps", "Retrieved " + repList.size() + " reps");
                } else {
                    Log.d("Reps", "Error: " + e.getMessage());
                }
            }
        });

        importList = repList;

I first want to make sure I'm importing from the current device, so I need to check if the Device field matches ParseInstallation.getCurrentInstallation(). Then I want to go ahead and get the first Reps array. However the last line importList = repList; does not work.

How can I go about achieving what I'm trying to do?

1
  • What does "does not work" mean? Commented Jul 6, 2015 at 2:38

2 Answers 2

1

query.findInBackground works in asynchronous way. In other words, the line that you set the importList is executed after the line query.findInBackground. However, the query.findInBackground will make a network call that takes time. So if you want to use the repList when it is ready, you have to use it in done method where you are use the network call is done. Hope this helps.

Regards.

Sign up to request clarification or add additional context in comments.

Comments

0

As @kinkspeech mentioned you need to move your line importList = repList; to your callback. And I suggest that you change it as follows:

query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> repList, ParseException e) {
            if (e == null) {
                Log.d("Reps", "Retrieved " + repList.size() + " reps");
                importList.addAll(replist);
            } else {
                Log.d("Reps", "Error: " + e.getMessage());
            }
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.