0

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)
AL.
  • 36,815
  • 10
  • 142
  • 281
AlwaysConfused
  • 729
  • 2
  • 12
  • 37

2 Answers2

1

As u now know that onDataChange() is executed asynchronously so i ll suggest that make checkOnline(String fId) return void and call a function like checkOnlineResponse(ifOnline) in onDataChange() and perform rest of your logic in the checkOnlineResponse(ifOnline) function. Hope it helps.

sanidhya pal
  • 345
  • 1
  • 9
0

It is because onDataChange is called asynchronously and the statement return ifOnline is executed before onDataChange has been called. Of course, provided that everything is fine with your database.

Dusan Jovanov
  • 547
  • 5
  • 21
  • How is that? onDataChange is inside checkOnline function, so onDataChange has to assign the value before checkOnline returns anything? – AlwaysConfused Apr 24 '17 at 17:14
  • No, `onDataChange` is a method of `ValueEventListener` object, which you are passing to the `addValueEventListener` method by an anonymous class. And the `ValueEventListener` will call the `onDataChange` method when it is done downloading from the database, and all of that is being executed asynchronously (in a different thread). So you cant rely that `onDataChange` will be called when you are calling `checkOnline` – Dusan Jovanov Apr 24 '17 at 17:19
  • You are right. I have implemented the code differently and it works fine now. I got rid of the function entirely and added onDataChange directly to another function from where the checkOnline was called – AlwaysConfused Apr 24 '17 at 18:21