I am trying to make it so that I can easily fetch the value of any path in my Realtime Database
. I had 2 different ways of doing it. They are below.
Method 1:
public Task<DataSnapshot> getValue(String path) {
return database.getReference(path).get();
}
Method 2:
public Object fetch(String path) {
Object value;
Task<DataSnapshot> task = database.getReference(path).get();
while (!task.isSuccessful());
value = task.getResult().getValue();
return value;
}
For whatever reason, if I add an onCompleteListener
to the output of the first function and do all of my code inside it, it works perfectly fine, but if I try and wait for it to be successful (method 2), it just gets stuck on the while loop forever. What is even weirder is that the exact code worked perfectly fine on a different project, so I am not really sure what's wrong.
I would totally just use method one except it doesn't always complete it at the same speed the rest of the program is going, so then the program ends up trying to get a method that hasn't been given its value yet.
If anyone has any ideas on how to get Method 2 to work please do share