How to Return Datasnapshot Value as a Result of a Method

How to return DataSnapshot value as a result of a method?

My function iss returning a value before the code in the inner class finishes

From the docs:

onDataChange: This method will be called with a snapshot of the data at this location. It will also be called each time that data changes.

When the data changes, whatever is inside the method will be executed. In other words, the method onDataChange instructs the program what to do when it gets results (which could be whenever)

This is asynchronous behaviour - as @Turing85 points out, this will only be called when the data changes.

If you want to know if something with this key exists, you could construct a query and then call get() method which would return a Task<DataSnapshot>. For this task, you could attach addOnCompleteListener listener to do what you would like. This would also be asynchronous, so it would only run when there is a result.

Unable to return Same Value from method,From its child method

When you call addValueEventListener() on the DatabaseReference, you are adding a listener that will have it's onDataChange() method fired when the data snapshot is ready (or the onCancelled() will be fired if there was an error).

These events are asynchronous and they probably happen only after you return your from your method and that's why you are getting the empty list.

The simplest way to get the results you need would be using a callback to do work when the results are ready.

Here's a great answer which you can refer to.

Order of output for log.d?

They aren't happening in the wrong order. Your code is asynchronous. The ValueEventListener is called after the data is gathered on a background thread. So they can happen in any order- it would be a race condition between which one gets called first. (In more practical terms, the "Called from another class" will almost always happen first in the code above because there's little enough code on that thread that it's unlikely the other thread will be scheduled and run before the main thread gets there, but you may see it happen once in a while).

Error in reading the Firebase: from firebase value to child

String price = String.valueOf(db.child("User").child("pricecode")); and instead of the value "1000", it writes a reference to the key there.

That's the expected behavior since the following operation:

String price = String.valueOf(db.child("User").child("pricecode"))

Does not store in the price variable the actual value (1000) of the pricecode field. The code inside the valueOf() method is a reference, so when you're passing that reference to the valueOf() method, you get:

https://testkornze...

So there is no way you can read a value of a particular field that exists in the Realtime Database without attaching a listener. I answered earlier a question of yours on how to read the value of pricecode. So to be able to use the value of pricecode, all operations that need data from the database should be added inside the onComplete() method.



Related Topics



Leave a reply



Submit