1

I am having trouble with Firebase, and I'm not sure exactly why. So I've read that it is good practice when using Firebase to grab the current user by their ID, with: FirebaseAuth.getInstance().getCurrentUser().getUid();, and then use a addListenerForSingleValueEvent to grab the data via datasnapshot. Great, in one of my activities, the code worked and I successfully grabbed the data. However, in my other activity the listener is completely ignored according to the debugger...why is this, and how should I fix this? Or, is there a better way?

Here is my activity:

public class GroupActivity extends AppCompatActivity {
//Tag
private static String TAG = "GroupActivity";

//Firebase
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference mDatabase = database.getReference("users").child("groups"); //Does Users -> Groups
private DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group);
    ButterKnife.bind(this);
    initializeBottomBar();

    //Users from selected list -- previous activity data.
    selectedUsers = (List<UserList>) getIntent().getSerializableExtra("users");
    String groupName = getIntent().getStringExtra("groupName");

    //CURRENT USER GOES IN FIRST.
    // TODO : add current user.
    String currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference newRef = mRef.child(currentUserID);
    newRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG, "Name: " + dataSnapshot.child("name").getValue());

            name = String.valueOf(dataSnapshot.child("name").getValue());
            email = String.valueOf(dataSnapshot.child("email").getValue());
            username = String.valueOf(dataSnapshot.child("username").getValue());
            ID = String.valueOf(dataSnapshot.child("uid").getValue());

            mCurrentUser.setName(name);
            mCurrentUser.setUsername(username);
            mCurrentUser.setEmail(email);
            mCurrentUser.setUid(ID);

            selectedUsers.add(mCurrentUser);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


    //ID
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    String uniqueKey = ref.child(currentUserID).child("groups").push().getKey();

    //Groups of SelectedUsers!
    SelectedUsers members = new SelectedUsers(groupName, uniqueKey, selectedUsers);
    List<SelectedUsers> groups = new ArrayList<>();
    groups.add(members);

Oh, and if you're wondering why I have multiple DatabaseReferences it's because I was testing something to better understand what was going on, that's all.

UPDATE:

I've decided to try debugging selectedUsers.add(mCurrentUsers) by adding a breakpoint while inside of the ValueEventListener, and it shows 2 items, as appropriate.

However, when I debug outside of the ValueEventListener it's missing the current user. Why is this happening?? I've had people suggest trying the mAuthListener, but you cannot grab a datasnapshot so this almost seems useless unless you want the user's email and displayname which firebase holds for you... help? I need the database information NOT the current user's login/authentication information!

Andrew Magana
  • 64
  • 2
  • 9

3 Answers3

0

Try moving your getInstance, getReference etc. statements to your OnCreate section. Only declare your variables before OnCreate, but don't already instantiate them there.

Ok, perhaps you can analyze the error by addind this to the onCancelled method:

Log.d("onCancelled: ", databaseError.toException());

Actually, where/how is mCurrentUser declared or created?

Cuculus
  • 166
  • 1
  • 2
  • 10
0
Firebase ref = new Firebase(FIREBASE_URL);
ref.child("MAIN_NODE_NAME/S3l3yRh8Tq.....").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.e("!_@@<DATA>",dataSnapshot+");
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("MainActivity", "onCancelled", firebaseError.toException());
            }
        });
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
0

The solution to this problem in case anyone ever runs into it is that this is a phenomenon known as asynchronous loading. In other words, the order of execution is what's causing my error because the database is not being read quick enough. For further explanation please follow read this thread.

The solution to my problem is to put everything needed to fill the recyclerview inside of onDataChange. This will allow the recycerlview to update on time of the application loading.

Thank you everyone and anyone who took the time to try and help me out!

Community
  • 1
  • 1
Andrew Magana
  • 64
  • 2
  • 9