0

I have multiple buttons in my android application and when clicked it has to fetch data from Firebase database and the fetched data is added into a list which has to be passed as an argument to another class constructor . But the fetching of data happens in another thread by default since firebase queries are asynchronous . How do I make sure that list has been added with data from other thread before passing it to constructor ?

Rahul Chandrabhan
  • 2,495
  • 5
  • 21
  • 32
  • Check [this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774) out. – Alex Mamo Feb 25 '19 at 10:15

1 Answers1

0

It's simple use oncompletelistener you can refer this example:

db=FirebaseFirestore.getInstance();
                db.collection("Order").document(TableListFragment.tableno)
                        .update(
                                "Items", FieldValue.arrayUnion("ABCD"),
                                "Quantity", FieldValue.arrayUnion("34")
                        ).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast.makeText(getContext(),"Item Added",Toast.LENGTH_LONG).show();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                   Log.i("onFailure:",e.toString());
                    }
                });
Ticherhaz FreePalestine
  • 2,183
  • 4
  • 18
  • 42
Mr. Patel
  • 1,329
  • 7
  • 21