I have this function:
public String checkOnline(String fId) {
DatabaseReference onlineUser = FirebaseDatabase.getInstance().getReference().child("online").child(fId);
onlineUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Online post = dataSnapshot.getValue(Online.class);
uOnline = post.timestamp;
different = getDate().getTime() - uOnline;
different = TimeUnit.MILLISECONDS.toMinutes(different);
if(different < 1){
ifOnline = "donline.setVisibility(View.VISIBLE);";
} else {
ifOnline = "donline.setVisibility(View.GONE);";
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
return ifOnline;
}
ifOnline is a string
I call checkOnline function and the function executes for each user and gets both statements (1 for each user of course).
The problem is that checkOnline returns null even the statements were executed just fine. Where is the problem? Is it because onDataChange is void?
I have no idea where the problem is as the ifOnline is private to the class, therefore as soon as it is assigned to the value it should return the currect value..
Data structure added:
Online database structure:
Online -> userId: status (string)
timestamp (long)