0

in my code I have a list of products barcode and in my Firebase Database I have values inside this barcodes, like this: BarCode structure.

I'm trying to retrieve the barcodes datas in a loop, but I cannot handle with this. Here is my code:

for (int i = 0; i < listaBarCodeProdutos.size() ; i ++ ){
            String barCode = (String) listaBarCodeProdutos.get(i);
            Query query = produtosRef.orderByKey().equalTo(barCode);
            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                        //Limpa o hash map
                        listaMercadosCB.clear();
                        //Salva os mercados e os preços e um hashmap
                        listaMercadosCB = (HashMap) snapshot.child("mercados").getValue();
                        //This is to parse the HashMap keys to Array                         
                        Object[] listaIdsCB = listaMercadosCB.keySet().toArray();


                        //add esse obj em um array de objetos
                        listaCB.add(listaIdsCB);
                        Log.i("listaCB1", String.valueOf(listaCB));
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

This code saves all data in barcode child in a HashMap, but when I try to get this HashMap value using Log.i("listaMercadosCB", String.valueOf(listaMercadosCB)); after the loop, it returns null...

This is the logcat for the values: 02-13 19:44:17.146 13903-13903/com.example.mts_rodrigues.projetocartolada I/listaMercadosCB: {wrxYaHZuLBTfrPrrvThxQREFzK02=4.5, d63vvncs2gh6PSnxFAjxhTOayyq1=2.9}

This is inside the loop

02-13 19:44:17.066 13903-13903/com.example.mts_rodrigues.projetocartolada I/listaMercadosCB: {}

This is after de loop

I guess that the value outside the loop is called before the Firebase Database retrieved him inside the loop...

All this codes are inside the onCreate() method. Anyone can help me ? Thanks!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This sounds like things are working as expected: the data isn't available until `onDataChange` has executed, so you can't use it before that. Most likely you'll need to move the code that needs the data **into** `onDataChange()`. Also see my answer here: https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Feb 13 '18 at 20:23
  • Thanks :D it's very helpful – Luis Guilherme Rodrigues Feb 16 '18 at 04:39

1 Answers1

0

This is not the correct way in which you can use the data that you get from a Firebase Realtime database. You are guessing right. onDataChange() method has an asynchronous behaviour, which means that your data si not available until this method is executed. A quick solution would be to use that data only inside onDataChange() method, or if you want to use it outside, dive in the asynchronous world and use the last part of my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193