Skip to content Skip to sidebar Skip to footer

Can I Retrieve Two Documents From The Database In Two Different Functions, Simultaneously?

I'm writing an app in Android Studio. It connects to Firebase Firestore. The data is structured like this (I've removed the irrelevant bits, for clarity): Boxes - box1 - g

Solution 1:

To solve this, I suggest you add a Grid and a Table object within a Box object. In this way you'll be able to a use a single listener instead of three. So your code should look like this:

boxDocRef = databaseRef.collection("boxes").document(box_id);
boxDocRef.get().addOnCompleteListener(newOnCompleteListener<DocumentSnapshot>() {
    @OverridepublicvoidonComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshotdocument= task.getResult();
            FragmentManagerfragmentManager= getSupportFragmentManager();
            BoxFragmentboxFragment= fragmentManager.findFragmentById(R.id.fragment_box);
            if (document.exists()) {
                // Set up Box
                box = document.toObject(Box.Class);
                if (box != null) {
                    // Set up some box views// Get grid/table keyGridgrid= box.getGrid();
                    boxFragment.setGrid(grid);
                    Tabletable= box.getTable();
                    boxFragment.setTable(table);
                }
            }
        }
    }
});

And remove the declaration of your FragmentManager and BoxFragment. Both should only be present inside the onComplete() method.

Post a Comment for "Can I Retrieve Two Documents From The Database In Two Different Functions, Simultaneously?"