Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

android - 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
      - grid_id
      - table_id
   -box2
      - grid_id
      - table_id
Grids
    - grid1
    - grid2
Tables
   - table1
   - table2

So, each time I retrieve a box, I also need to retrieve the corresponding Grid and Table.

In my Activity, I have the following:

public class BoxViewActivity {

   String box_id;
   Box box;
   FirebaseFirestore databaseRef;
   BoxFragment boxFragment;
   GridFragment gridFragment;
   TableFragment tableFragment;
   FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_edit_box);
        fragmentManager = getSupportFragmentManager();
        boxFragment = fragmentManager.findFragmentById(R.id.fragment_box);

        // Retrieve Box from database and set it up
        Intent intent = getIntent();
        box_id = intent.getStringExtra("BoxId");
        box = new Box();

        databaseRef = FirebaseFirestore.getInstance();

        boxDocRef = databaseRef.collection("boxes").document(box_id);
        boxDocRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        // Set up Box
                        box = document.toObject(Box.Class);
                        if (box != null) {
                            // Set up some box views
                        }
                        // Get grid/table key
                        setUpGrid(box.getGridId());
                        setUpTable(box.getTableId());
                    }
                }
            }
        });
    }

    private void setUpGrid(String gridId) {

        DocumentReference gridRef = databaseRef.collection("grids").document(gridId);

        gridRef.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        // Retrieve Grid
                        Grid grid= document.toObject(Grid);
                        if (grid != null) {
                            boxFragment.setGrid(grid);
                        }
                    }
                }
            }
        });
    }

    private void setUpTable(String tableId) {
        if(tableId == null) return;

        DocumentReference tableRef = databaseRef.collection("tables").document(tableId);

        tableRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        Table table = document.toObject(Table.Class);
                        if (table != null) {
                            boxFragment.setTable(table);
                        }
                    }
            }
        });
    }

}

The problem I'm having is that the app seems to 'freeze' and then restart when I hit this activity. The screen goes blank and unresponsive, and then the app restarts. Android Studio isn't throwing any errors, and the debug console has nothing. I suspect it's because I'm retrieving the grid and table simultaneously - would that slow it down? If so, what's a better way to do it? Or have I missed something else?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...