Ravers Ravers - 4 months ago 14
Android Question

Can I iterate in the background?

I have a parse query that is freezing my application:

ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle");
query.whereEqualTo("puzzle", "somePuzzle");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
ArrayList<Puzzle> listPuzzle = new ArrayList<>();
for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));

ListView list = (ListView) findViewById(R.id.list_puzzle);
if (list != null && listPuzzle.size() != 0) {
AdapterPuzzle adapterPuzzle = new AdapterPuzzle(listPuzzle, ScreenPuzzle.this);
list.setAdapter(adapterPuzzle);
}
} else e.printStackTrace();
}
});


When I do this query, the activity freezes for some seconds until I have my ListView filled.

I tested to run the query without the content in the method "done" and it seems to run smoothly, so my guess is that my actions inside the "done" method are freezing the activity because it may be doing to much work, specially the iterator:

for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));


Is there any way to run this iterator or all this actions in the background? Any way to avoid this freeze?

Answer

Try to use AsyncTask class. It has doInBackground method axactly for your task.

EDIT:

I'm adding the solution for my code for someone that want some reference:

public class ScreenPuzzle extends AppCompatActivity {

    private ListView list;
    private TextView textUnresolved;
    private ProgressBar loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_puzzle);

        list = (ListView) findViewById(R.id.list_puzzle);
        textUnresolved = (TextView) findViewById(R.id.text_unresolved);
        loading = (ProgressBar) findViewById(R.id.loading_rank);

        ParseQuery<ParseObject> query = new ParseQuery<>("Puzzle");
        query.whereEqualTo("puzzle", "somePuzzle");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) new BackgroundOperation(objects).execute();
                else e.printStackTrace();
            }
        });

    private class BackgroundOperation extends AsyncTask<Void, Void, ArrayList<Rank>> {

        private List<ParseObject> objects;
        private ArrayList<Rank> listPuzzle;

        public BackgroundOperation(List<ParseObject> objects) { this.objects = objects; }

        @Override
        protected ArrayList<Rank> doInBackground(Void... voids) {
            listPuzzle = new ArrayList<>();
            for (ParseObject object : objects) listPuzzle.add(new Puzzle(object));

            return listRank;
        }

        @Override
        protected void onPostExecute(ArrayList<Rank> listRank) {
            if (list != null && listRank.size() != 0) {
                final AdapterPuzzle adapterPuzzle = new AdapterPuzzle(listPuzzle, ScreenPuzzle.this);
                list.setAdapter(adapterPuzzle);
            } else textUnresolved.setVisibility(View.VISIBLE);

            loading.setVisibility(View.GONE);
        }
    }
}
Comments