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!