Search code examples
androidfirebaseandroid-asynctaskgoogle-cloud-firestore

Async with Firestore


I'm trying to make ProgressBar rotate, while data from Firestore is loading into the ListView. And then, after loading - the ProgressBar should disappear.

That's how I tried to do it, but in multithreading I'm still a complete zero.

It's inner class, where I try to use async:

ProgressBar prog;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    prog = findViewById(R.id.progressCircle);
    prog.setVisibility(View.INVISIBLE);
    class MyProgress extends AsyncTask<Void, Integer, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            prog.setVisibility(View.VISIBLE);
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            prog.setVisibility(View.INVISIBLE);
        }
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
        @Override
        protected Void doInBackground(Void... voids) {
            query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        List<Places> placesList = new ArrayList<>();
                        for (DocumentSnapshot document : task.getResult()) {
                            Places places = document.toObject(Places.class);
                            System.out.println(places);
                            placesList.add(places);
                        }
                        if (placesList.size() > 0) {
                            System.out.println(placesList);
                            int placeCount = placesList.size();
                            Random randomGenerator = new Random();
                            ArrayList<Places> randomPlaceList = new ArrayList<>();
                            for (int i = 0; i < 3; i++) {
                                int randomIndex = randomGenerator.nextInt(placeCount);;
                                Places item = placesList.get(randomIndex);
                                randomPlaceList.add(item);
                            }
                            ListView mListView = findViewById(R.id.place_list);
                            mAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
                            mListView.setAdapter(mAdapter);
                        }
                    } else {
                        error.setVisibility(View.VISIBLE);
                    }
                }
            });
        return null;
    }
}

And layout of activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card_new_bg">

    <ProgressBar
        android:id="@+id/progressCircle"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <ListView
        android:id="@+id/name_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true" >
    </ListView>
</RelativeLayout>

Solution

  • It's rather redundant to use async task with firebase calls which are async by design.

    What you can simply do is to create two methods:

    public void showLoading(){
      prog.setVisibility(View.VISIBLE);
    }
    
    public void hideLoading(){
        prog.setVisibility(View.GONE);
    }
    

    And right before calling your async call to firebase call these methods.

    When you receive the result call the hiding method:

    public void queryFirebase() {
        showLoading();
        query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    List<Places> placesList = new ArrayList<>();
                    for (DocumentSnapshot document : task.getResult()) {
                        Places places = document.toObject(Places.class);
                        System.out.println(places);
                        placesList.add(places);
                    }
                    if (placesList.size() > 0) {
                        int placeCount = placesList.size();
                        Random randomGenerator = new Random();
                        ArrayList<Places> randomPlaceList = new ArrayList<>();
                        for (int i = 0; i < 3; i++) {
                            int randomIndex = randomGenerator.nextInt(placeCount);;
                            Places item = placesList.get(randomIndex);
                            randomPlaceList.add(item);
                        }
                        ListView mListView = findViewById(R.id.place_list);
                        mAdapter = new PlaceAdapter(randomPlaceList, getBaseContext());
                        mListView.setAdapter(mAdapter);
                    }
                } else {
                    error.setVisibility(View.VISIBLE);
                }
                hideLoading();
            }
        });
    }
    

    Then you just do:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        prog = findViewById(R.id.progressCircle);
        prog.setVisibility(View.INVISIBLE);
        queryFirebase();
    }