perlponderer
perlponderer

Reputation: 43

Storing all ParseFiles for a given ParseUser?

I'm new to Parse and was wondering if there is any way to store all the ParseFiles (in this case images) for a given ParseUser into something like an ArrayList?

Here's my code:

public ArrayList<ParseFile> getFiles() {

    ArrayList<ParseFile> files = new ArrayList<ParseFile>();

    //mUser is the current ParseUser
    ParseQuery<ParseObject> query = ParseQuery.getQuery(mUser.getUsername());
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> fileList, ParseException e) {
            if (e == null) {
                Log.d("FILES", "Retrieved " + fileList.size() + " files");

                for(ParseObject ch:fileList) {


                    image = ch.getParseFile("image");
                    files.add(image);

                        @Override
                        public void done(byte[] arg0, ParseException arg1) {
                            //nothing to do here                    
                        }
                    });

                }
                Log.i("TAG", ": "  + files.size());


            } else {
                Log.d("FILES", "Error: " + e.getMessage());
            }
        }

    });
    Log.i("DONE", ": " + files.size());

    return files;
}

When I'm inside done(), the 3 images I have for this particular user are added and I get an ArrayList of size 3 which is to be expected. When I'm outside of done(), the size of the ArrayList goes back to 0, which I'm assuming is because it's being referenced outside of the query. And sure enough it returns an empty ArrayList (not too shocking).

I feel like this should be an easy solution but I can't seem to figure it out. I've tried to make a static ArrayList variable but that doesn't seem to work either. Any idea on how to return the desired ArrayList?

Upvotes: 0

Views: 194

Answers (2)

KP_
KP_

Reputation: 1866

Try like this

ParseQuery<ParseObject> query = ParseQuery.getQuery(mUser.getUsername());
List<ParseObject>imageList=query.find();

try {

Arraylist<ParseFile> files = new  Arraylist<ParseFile>files();

 ParseFile image;

      for(int i =0;i<imageList.size();i++)

 {

          image = imageList.get(i).getParseFile("image");
          files.add(image);

 } 

}

catch()
{
}

Upvotes: 0

dispake
dispake

Reputation: 3329

I believe the problem is that the outside thread still continues before your background process is completed. In other words..

 1. query.findInBackground(....);
 2. Log.i("DONE" ....);

.. 2. is executed before 1. completes. The whole point of Parse "inBackground" is that it completes actions that your thread is not dependent on. If you need to do something with the List, you should do it in the same thread as the background thread or not do it "inBackground".

Upvotes: 1

Related Questions