I have two types of users in my application a customer and a deliveryman, The customer sets a value in a specific deliveryman in the database and the deliveryman listens to that value and should accept or reject the customers request in another value. What i need is to set the listener in the customer side and then make him wait for a 15 seconds for the deliveryman response before moving to another deliveryman, how to do that?
Asked
Active
Viewed 357 times
1
-
asynchronous tasks – Jun 01 '18 at 19:47
-
@Eminem can you explain more how can I use it in this case? – Toka A. Amin Jun 10 '18 at 10:39
-
can you show me your database structure? – Jun 10 '18 at 15:33
-
@InnerFire I added it to the question. – Toka A. Amin Jun 11 '18 at 06:01
-
please add it in this way::`online_deliveryMan->id->...`add also your users structure too,you do not need to wait 15 seconds for the answer you have to wait for the users asnwer which btw may take more than 15 seconds,using a asynchronic task you can make it wait here is an example https://stackoverflow.com/a/9671602/9025311 – Jun 11 '18 at 06:57
1 Answers
0
You can use postDelayed
with Firebase valueEvent Listener.
declare global runnable variable in your activity.
final Handler handler = new Handler(Looper.getMainLooper());
Runnable runnable = null;
Then add value event listener on the node you want to read.
in the callback onDataChange
check if snapshot doesn't exist and runnable variable was null, set timer.
new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists() && runnable == null) {
// initialize runnable
// handler.postDelayed(runnable,15000)
} else
{ //cancel the runnable if not null so that it don't get triggered}
}
@Override
public void onCancelled(DatabaseError databaseError) { }
}

Umar Hussain
- 3,461
- 1
- 16
- 38
-
I kinda have another problem, the code never executes onDataChange or onCancelled in the valueEventListner. And it goes into an infinite loop. Will the timer solve this problem? We tried your answer but i we can't really know if it worked or now because it never gets executed. – Toka A. Amin Jun 16 '18 at 18:56