Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 436 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't read Firebase Database

#1
This is my code for reading Firebase data:

final String[] booknum = {"0"};
databaseReference.child("All BID").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
booknum[0] =Long.toString(snapshot.getChildrenCount());
Toast.makeText(getApplicationContext(), booknum[0],Toast.LENGTH_LONG).show();

}
@Override public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
Toast.makeText(getApplicationContext(), booknum[0],Toast.LENGTH_LONG).show();

When I am executing this, the first toast(inside ValueEventListener) prints the right answer (eg '8'). But the toast outside always prints 0 no matter what.

Please Help!
Reply

#2
The ValueEventListener is asynchronous. The lines of code inside of the listener, including the assignment of booknum[0], are not guaranteed to execute before the lines of code written outside of the listener. If you depend on the new value of booknum[0] for some operation, consider moving that operation inside of onDataChange() to ensure it uses the new value.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through