Cloud Firestore can’t get data out of onComplete

Asked

Viewed 43 times

0

So my code is this::

SpinnerDialog spinner;
String[] textoSeparado;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_item);
    resultado = findViewById(R.id.result);
    DocumentReference docRef = db.collection("Bebidas").document("Cervejas");

    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        public void onComplete(Task<DocumentSnapshot> task) {

            String texto = task.getResult().getData().toString();
            texto = texto.replaceAll("[^a-zZ-Z1-9 ]", "");
            textoSeparado = texto.split("null");



        }

    });
        resultado.setText(textoSeparado[0]);



}

But for some reason it says that the n array has data. if setText is placed inside onComplete works. I don’t know how to play the firestore, any ideas?

1 answer

1


The search for data from Firestone is done asynchronously, ie you request the data, and when they "arrive" (when the operation is completed) the part of the code that is in the onComplete is executed. However, your program does not get stuck waiting for this data, the code keeps running. That’s why your setText is not working, the data have not yet arrived and he is already trying to set the text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.