0

I want to extract database from Firebase. In for each loop, it shows me the description from database but after the for each loop when I check it again for that description, it shows me null and not only for description but for all other values also. Here is the code for extracting data from Firebase. It doesn't shows any kind of error. Help me.

public void getDatafromFirebase(){
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                DataSnapshot Videoreference = dataSnapshot.child("Videos");
                for (DataSnapshot videoschild : Videoreference.getChildren()){
                    DataSnapshot description = videoschild.child("Description");
                    DataSnapshot duration = videoschild.child("Duration");
                    DataSnapshot title = videoschild.child("Title");
                    DataSnapshot thumbnail = videoschild.child("Thumbnail");
                    details.setDescription(String.valueOf(description.getValue()));
                    details.setDuration(String.valueOf(duration.getValue()));
                    details.setTitle(String.valueOf(title.getValue()));
                    details.setThumbnail(String.valueOf(thumbnail.getValue()));
                    Log.e("details",details.description);
                    list.add(details);
                }
                Log.e("Size",list.get(0).description);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Where is `details` declared? Also where is `list` declared? It looks as though your issue might be that you're using the same `details` object for each loop iteration so the data's being overwritten. It's hard to tell without knowing where they're declared though. – SamCosta1 Sep 08 '18 at 08:15
  • They are declared before the class – Pratham Khurana Sep 08 '18 at 09:20
  • Could you post the full code including where they're declared? It does look like that's what's happening though. I think you might also have an async issue. The on data change callback is async. So if you're calling `getDatefromFirebase()` then on the next line checking the list, it will be empty at that point since the async callback hasn't run yet. – SamCosta1 Sep 08 '18 at 09:37

1 Answers1

0

If I understand correctly, you are trying to log the value of your description property (or any other property), outside the callback and you are getting null, right?

This ia happening because Firebase APIs are asynchronous, meaning that onDataChange() method returns immediately after it's invoked and the callback from the Task it returns, will be called some time later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available. Because that method returns immediately, the value of your description property you're trying to use it outside the onDataChange() method, will not have been populated from the callback yet.

Basically, you're trying to return a value synchronously from an API that's asynchronous. That's not a good idea. You should handle the APIs asynchronously as intended.

A quick solve for this problem would be to use the description value only inside the onDataChange() method, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

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