Handler in addValueEventListener running continuously

56
June 21, 2022, at 11:40 AM

Im making a Uber clone app and whenever the driver receives a request he can accept it or decline it, but I want to add a feature where if the driver doesn't accept or decline the request within 8 second I want to decline it automatically.

Let me explain my code, I'm creating a DatabaseReference checkJobRequest that will point to the current driverId and I'm actively listening if a child called dataSnapshot.hasChild("jobRequest") appears, if it does appear that means there is a request for the driver so I'm going to create another listener and check the status of that request String valueOfStatus = (String) snapshot.child("requestStatus").getValue(); now if the status is "waiting" I'm going to display a dialog where I ask the driver to accept it or decline it.

Now the problem is when I try to implement a handler that is going to decline the request automatically after 8 seconds, the handler never stops running because its inside the checkJobRequest.addValueEventListener, and this listener is always running in the background checking for a request. I tried killing the handler handler.removeCallbacksAndMessages(null); but since the listener runs again and again it will start it again and again like a loop.

can someone suggest a workaround this problem?

private void jobRequest(){
    // clicked=false;
    String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference checkJobRequest = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverId);
    checkJobRequest.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.hasChild("jobRequest") && !isCurrentlyWorking){
                DatabaseReference checkStatusValue =FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverId).child("jobRequest");
                checkStatusValue.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        if(snapshot.hasChild("requestStatus")) {
                            String valueOfStatus = (String) snapshot.child("requestStatus").getValue();
                            String waiting = "waiting";
                            if (valueOfStatus.equals(waiting))
                                mJobRequest.setVisibility(View.VISIBLE);
                            else mJobRequest.setVisibility(View.GONE);
                        }
                    }
                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                    }
                });
                mRideAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        clicked = true;
                        isCurrentlyWorking = true;
                        String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid();
                        mJobRequest.setVisibility(View.GONE);
                        checkJobRequest.child("jobRequest").child("requestStatus").setValue("accepted");
                        DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverId);
                        driverRef.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                if(snapshot.hasChild("customerRequest"))
                                getAssignedCustomer();
                            }
                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {
                            }
                        });

                    }
                });
                mRideDecline.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        isCurrentlyWorking = false;
                        clicked=true;
                        mJobRequest.setVisibility(View.GONE);
                        checkJobRequest.child("jobRequest").child("requestStatus").setValue("declined");
                    }
                });

                    Handler handler = new Handler();
                    final Runnable r = new Runnable()
                    {
                        public void run()
                        {
                            //isCurrentlyWorking = false;
                           // clicked=true;
                            mJobRequest.setVisibility(View.GONE);
                            checkJobRequest.child("jobRequest").child("requestStatus").setValue("declined");
                        }
                    };
                    handler.postDelayed(r, 1000);

            }else mJobRequest.setVisibility(View.GONE);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
}
READ ALSO
create SP in sybase and call it from java by passing List<XModel> to it

create SP in sybase and call it from java by passing List<XModel> to it

I have a pojo class XModeljava with fields -> f1, f2, f3, f4, f5, f6 all string and

64
Is there any way to inject Interceptor from external library in Spring?

Is there any way to inject Interceptor from external library in Spring?

I'm developing a jar library and trying to inject an interceptor from external jar library to Application

52
Clear/unset a system property maven.compiler.release so that is is no longer present

Clear/unset a system property maven.compiler.release so that is is no longer present

I have a project with complicated build setup (parent POM's with parent POM's) and so it happens that my compiler plugin executes with mavencompiler

47
Include project modules in a single jar

Include project modules in a single jar

I have a multi module maven project, the 3 modules are api, impl, allThe all module is a placeholder and has no source code, it only has a pom

32