0

I am in the need of update a static variable. I am getting the value from Firebase, but it seems outside the addListenerForSingleValueEvent the static variable is not updated. Where I am wrong?

Flom logcat:

02-02 18:16:47.146 8329-8329/xxx E/query_result: null <-- wrong
02-02 18:16:47.675 8329-8329/xxx E/query: Dogee <-- correct

Code:

class FirebaseUtil {

    public static String nameOfCurrentUser;

// other code . . . 

   public static Author getAuthor() {

        // Is the current user running the app registered?
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user == null) return null;
        DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

        // 20180202 Add the listener on db /users/$currentUID
        mDatabase.child("users").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                // Getting the child full_name of the user (mDatabase.child("users").child(user.getUid()))
                nameOfCurrentUser = (String)dataSnapshot.child("full_name").getValue();
                Log.e("query",nameOfCurrentUser);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                Log.w("DB read error", "loadPost:onCancelled", databaseError.toException());
                // ...
            }
        });

        if (nameOfCurrentUser == null) {
        Log.e("query_result","null");

As you can see once I am out of the method the variable nameOfCurrentUser is != null inside the method, null outside.

czane
  • 392
  • 1
  • 6
  • 18
  • Firebase works in asynchronous mode. hence the null part is logged first. query_result: null <-- wrong E/query: Dogee <-- correct May I know what you like to do with variable nameOfCurrentUser? – tanni tanna Feb 02 '18 at 19:55
  • I just need to call Author class which receives uid,nameOfCurrentUser,profilePic) . Data is already on the db and I just need to read that specific entry, not all the db. – czane Feb 02 '18 at 20:09
  • once you get the value, will you update any view (textview) or do you need to pass to some other method? – tanni tanna Feb 02 '18 at 20:12
  • need to call Author class which will give me back an Author object return new Author(nameOfCurrentUser, PicHere, user.getUid()); – czane Feb 02 '18 at 20:17
  • please see https://stackoverflow.com/questions/47729957/android-firebase-getting-null. I do not have option to post answer as this link address the issue, sorry. – tanni tanna Feb 02 '18 at 20:23
  • I see, but is that possible that to read just 1 value I have to make all this work? There is no straight way to read just 1 value form 1 key? I already know the full path to read from. – czane Feb 02 '18 at 20:26
  • to read you need not. But if you want to play with the value, then you have to as Firebase is asynchronous. Those listeners help in informing when the data is ready for use. i would suggest try that solution may be for current requirement it may look tedious. It will help you in future. – tanni tanna Feb 02 '18 at 20:30
  • If you are using arraylist and adapter then you can try this public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.hasChildren()){ doctorsArrayList.clear(); for(DataSnapshot drDetails : dataSnapshot.getChildren()){ doctors = drDetails.getValue(Doctors.class); doctorsArrayList.add(doctors); } doctorsAdapter.notifyDataSetChanged(); } – tanni tanna Feb 02 '18 at 20:31
  • Thanks, but I do not need to check for data changed. Data is never changed in that value, so I just need a read like .getValue(full_name); is that possible? – czane Feb 02 '18 at 20:35
  • sorry , unfortunately that is not possible. Read data once In some cases you may want a callback to be called once and then immediately removed, such as when initializing a UI element that you don't expect to change. You can use the addListenerForSingleValueEvent() method (which you have already implemented) https://firebase.google.com/docs/database/android/read-and-write – tanni tanna Feb 02 '18 at 20:40
  • Thanks for your help. Anyway it is very time consuming, was expecting something more straight forward as in Firestore to do a simple read. – czane Feb 02 '18 at 20:42
  • if these values are for only for the user who has installed the app and you are storing from the app, then try shared preference. These can save values even after app is close. You will loose them only when you explicitly change or uninstall the app. https://developer.android.com/training/data-storage/shared-preferences.html – tanni tanna Feb 02 '18 at 20:53

0 Answers0