1

I've a String variable from a database. I've an access in @Override onDataChange method.

But I'd like to see that value of this string in outside of this @Override method. At now, I cannot see, because value of this string from database is available only in @Override method.

    firebase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    CarClass carClass = dataSnapshot.getValue(CarClass.class);

                    string1 = carClass.getCarName();
                    // value of "string1" is = "name"

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
// here value of "string1" is empty.

Does someone could help me to resolve this problem?

EDIT:

I found solution myself:

To have an access of value of "string1" from "onDataChange method" I used SharedPreferences. I put this "string1" to SharedPreferences in @Override method, and I can get this value of "string1" outside of this method using SharedPreferences.

I show my explanation below:

     public static final String Car_Name = "Name_PREFS";
     public static final String Car_Key = "String_PREFS";
     SharedPreferences sharedPreferences;

      firebase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            CarClass carClass = dataSnapshot.getValue(CarClass.class);

                            string1 = carClass.getCarName();


    sharedPreferences = getApplicationContext().getSharedPreferences(Car_Name, Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(Car_Key, string1);
                    editor.apply();


                        }

                        @Override
                        public void onCancelled(FirebaseError firebaseError) {

                        }
                    });


    sharedPreferences = getApplicationContext().getSharedPreferences(Car_Name, MODE_PRIVATE);
            string1 = sharedPreferences.getString(Car_Key, null);
            // here value of "string1" exists outside of "onDataChange" method.
// string1 = "name"
    }
AL.
  • 36,815
  • 10
  • 142
  • 281
gryzek
  • 537
  • 9
  • 25

1 Answers1

1

Let me copy my answer from this question here: (Note that the question is not exactly the same but have similar answer)

Let me make it more simple by describing how your code doesn't work as you expected:

    firebase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ... (point 1)
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });

    ... (point 2)

Firebase Database load your data asyncronously. It means (to make it simple) that the process of getting data does not interfere with your main process. With that in mind, the code in (point 2) does not always get executed after code in (point 1), and vice versa. Though usually code in (point 1) get executed after (point 2), it is not always like that. So you should consider that the code you write in (point 1) can be executed at anytime.

Then you should write your code with that concept. Meaning that if you want to do anything to a variable inside (point 1) (like get value of string1), you should place it all inside (point 1)

Hope this help.

Note: JP Ventura mention in the comment (from linked question) that (point 2) is always executed after (point 1) as the callback onDataChange will take at least 4 milliseconds to run

Community
  • 1
  • 1
koceeng
  • 2,169
  • 3
  • 16
  • 37
  • I figured out with this using SharedPreferences. At now, only this solution work for me to using a string1 outside of this @Override method. – gryzek Jan 08 '17 at 11:33
  • Your solution above doesn't really solve your problem. See, `sharedPreference` is not cleared out whenever application closes. It means that your solution is just using `string1` data from previous time you open the application. Try clear data or re-install your application so that it run for the first time, and tell me if that works well :) – koceeng Jan 08 '17 at 12:36
  • Yes, You're right.. When app is closed and open again at the first time it's using string1 from the previous time. When I open activity again it shows properly value. So, how could I resolve this challenge? I tried your solution but I can't work too. Do you have some idea? – gryzek Jan 08 '17 at 13:36
  • I think my solution is better. Why didn't you try it out? And what exactly is you want to do with `string1`? Is it complicated algorithm? Or is it related to the view/layout? – koceeng Jan 08 '17 at 13:39
  • This string1 is a URL address for an image from the firebase database. I use it with Picasso in OnCreate method but outside from @Override onDataChange. I wanted use it your solution but it can't work with Picasso and context in this function. I've created outside function to fetch data from firebase and call it in OnCreate, and next I call Picasso method, which use string1 from sharedPref. – gryzek Jan 08 '17 at 13:40
  • Then your picasso initialization code should be placed inside `onDataChange`. – koceeng Jan 08 '17 at 13:42
  • I lost almost 10 hours how to manage with this string1 and SharedPreferences... But I initialized Picasso in this onDataChange with "getApplicationContext()" and now it works without SharePref. Btw. Thank you! – gryzek Jan 08 '17 at 13:49
  • Your solution is risky, my suggestion is to test it on all condition (like first install use). Anyway, happy coding :) – koceeng Jan 08 '17 at 13:54