0

Currently my app gets all the data from the realtime database and filters through it and sends it back to android where the data is shown in a recycler view. How do I paginate my data so the user does not have to wait for the whole thing?

Firestore Function:

exports.getRecipes = functions.region('europe-west1').https.onCall((data, context) => {

    const categories = data.categories;
    const ingredients = data.ingredients;

    recipesToReturn = [];

    const eventsData = Promise.all(categories.map((category) => {
        return admin.database().ref(category).once('value').then((snapshot) => {

            snapshot.forEach(function (childSnapshot) {
                if (canBeMade(childSnapshot.val(), ingredients)) {
                    recipesToReturn.push(childSnapshot.val());
                }
            });
            return recipesToReturn;

        });
    }));

    return eventsData;

});

Android Studio:

getRecipes().addOnCompleteListener(new OnCompleteListener<ArrayList<Recipe>>() {
            @Override
            public void onComplete(@NonNull Task<ArrayList<Recipe>> task) {

                if (r.size() <= 0) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(RecipesActivity.this, "Nema recepata", Toast.LENGTH_SHORT).show();
                    return;
                }
                for (int i = 0; i < r.size(); i++) {
                    recipes.add(r.get(i));
                    adapter.notifyItemInserted(i);
                }
                progressBar.setVisibility(View.GONE);

            }
        });

private Task<ArrayList<Recipe>> getRecipes() {
        Map<String, Object> data = new HashMap<>();
        data.put("categories", categories);
        data.put("ingredients", ingredients);

        return mFunctions
                .getHttpsCallable("getRecipes")
                .call(data)
                .continueWith(new Continuation<HttpsCallableResult, ArrayList<Recipe>>() {
                    @Override
                    public ArrayList<Recipe> then(@NonNull Task<HttpsCallableResult> task) throws Exception {
                        ArrayList<ArrayList<HashMap<String, Object>>> result = (ArrayList<ArrayList<HashMap<String, Object>>>) task.getResult().getData();
                        ArrayList<Recipe> recipes = new ArrayList<>();
                        for (int i = 0; i < result.size(); i++) {
                            for (int j = 0; j < result.get(i).size(); j++) {
                                recipes.add(new Recipe(result.get(i).get(j)));
                            }
                        }
                        return recipes;
                    }
                });
    }
AkiTheDev
  • 3
  • 4
  • I think that this [answer](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android) might help. – Alex Mamo Jan 17 '22 at 15:26
  • If you consider at some point in time to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), here you can find a useful article, [How to paginate Firestore using Paging 3 on Android?](https://medium.com/firebase-tips-tricks/how-to-paginate-firestore-using-paging-3-on-android-c485acb0a2df) might help. – Alex Mamo Jan 17 '22 at 15:27
  • The topic of pagination results from Firebase on Android was covered many times before. I recommend checking out some of the previous [questions on the topic](https://stackoverflow.com/search?q=%5Bfirebase-realtime-database%5D%5Bandroid%5D+pagination), and trying it yourself. If you get stuck, share a [minimal reproduction](http://stackoverflow.com/help/mcve) and we can probable help better. – Frank van Puffelen Jan 17 '22 at 16:07

0 Answers0