2

This is simple method for reading long value from one child in my database.

The same method also gives me null for pojo.

What am I doing wrong ??? Why OnDataChanged() is skipped?

(Path to child : UserData/DeviceMgmt/Average:)

here is the code:

public class DevicesUtil {
public final static FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
public final static DatabaseReference dbRefUserData = firebaseDatabase.getReference("UserData");
public final static DatabaseReference dbRefUsers = dbRefUserData.child("Users");
public final static DatabaseReference dbRefDisplayNames = dbRefUserData.child("DisplayNames");
public final static DatabaseReference dbRefDeviceMgmt = dbRefUserData.child("DeviceMgmt");
public final static DatabaseReference dbRefAverage = dbRefDeviceMgmt.child("Average");
public final static DatabaseReference dbRefDevices = dbRefDeviceMgmt.child("Devices");
public final static DatabaseReference dbRefNumberOfAll = dbRefDeviceMgmt.child("NumberOfAll");
public final static DatabaseReference dbRefNumberOfSelected = dbRefDeviceMgmt.child("NumberOfSelected");
public final static DatabaseReference dbRefTotalDownloaded = dbRefDeviceMgmt.child("TotalDownloaded");

    private static long theAverage;

    public static long getAverage() {
        dbRefAverage.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                theAverage = dataSnapshot.getValue(Long.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, databaseError.getMessage() + ", Exception: ", databaseError.toException());
            }
        });
        return theAverage;
    }

public static User getDbUser(final String uid) {
    // getting User value:
    dbRefUsers.child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange (DataSnapshot dataSnapshot) {
            userValue = dataSnapshot.getValue(User.class);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG, databaseError.getMessage() + ", Exception: ", databaseError.toException());
        }
    });

}}

and Json:

{

"UserData" : { "DeviceMgmt" : { "Average" : 666, "Devices" : { "-LEPhxstDLC-SJKITtCI" : { "downloaded" : 0, "failures" : 0, "fcmToken" : "dRneV", "signOutTime" : 0, "toSelect" : true, "uid" : "AsU7GKtuTSZFwm3ePwvk4s3AEbT2", "used" : 0 }, "Device" : "dummy" }, "NumberOfAll" : 8, "NumberOfSelected" : 8, "TotalDownloaded" : 0 }, "DisplayNames" : { "Pablo X" : "tWlLHuDgGVPr0ZivEEpIi4tik352", "Pablo XY" : "0HW3pcWHU9YuPYM4Od4YBqDzB8C3", "Pawel" : "AsU7GKtuTSZFwm3ePwvk4s3AEbT2", "displayName" : "dummy" }, "Users" : { "AsU7GKtuTSZFwm3ePwvk4s3AEbT2" : { "deviceKey" : "-LEPhxstDLC-SJKITtCI", "displayName" : "Pawel", "email" : "email@gmail.com", "profile" : "user", "signOutTime" : 0 }, "user" : "dummy" } } }

Pablo_xyu
  • 31
  • 1
  • 7
  • Can you add the JSON at the location that you query (as text, no screenshot please). You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 07 '18 at 22:31
  • And: is your `onDataChange` being called at all? – Frank van Puffelen Jun 07 '18 at 22:39
  • And console show some error? – Diego Venâncio Jun 07 '18 at 22:56
  • onDataChange() is not called. That's a problem – Pablo_xyu Jun 07 '18 at 23:01
  • You can't `return theAverage` from `getAverage()` because `onDataChange()` fires asynchronously. The method completes before `onDataChange()` runs. Perhaps that is making it appear that `onDataChange()` is not called. – Bob Snyder Jun 07 '18 at 23:25
  • So, how can I return that value from onDataChange ? I thought global variable was a good idea... As I understand the only way to get firebase child value is adding ValueEventListener with this callback... – Pablo_xyu Jun 07 '18 at 23:32
  • Ok. I spent 2 days on that. If anyone have an idea how to synchronise it... Europeans go to sleep...thx – Pablo_xyu Jun 07 '18 at 23:44
  • You cannot return something now that hasn't been loaded yet. Please check the duplicate to see why do you have this behaviour and how can you solve this using a custom callback. – Alex Mamo Jun 08 '18 at 07:45

0 Answers0