0

i tried using async but it doesnt work, searching around google also doesnt give me anything that works

right now, my value[0] gets returned before my docRef onComplete has finished executing resulting in always being true instead of checking

private boolean checkMax(String email){
        final boolean[] value = new boolean[1];
        value[0] = true;

        DocumentReference docRef = db.collection("user_bookings").document(email).collection(day).document("0");
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot doc = task.getResult();
                    if (doc.exists()) {
                        Long slot1 = doc.getLong("slot1");
                        Long slot2 = doc.getLong("slot2");
                        if(slot1 == -1 && slot2 == -1){
                            value[0] = false;
                        }
                        else if(slot1 == -1 || slot2 == -1){
                            if(durationPosition == 1){
                                Toast.makeText(BookingActivity.this, "Will exceed maximum booking, please reduce duration", Toast.LENGTH_SHORT).show();
                                value[0] = true;
                            }
                            else value[0] = false;
                        }
                        else{
                            Toast.makeText(BookingActivity.this, "You've reached your maximum booking for the selected day", Toast.LENGTH_SHORT).show();
                            value[0] = true;
                        }

                    } else {
                        value[0] = false;
                        Log.e(TAG, "value of value = " + Boolean.toString(value[0]));
                        Log.d(TAG, "No such document in CHECKMAX");
                    }
                } else {
                    value[0] = true;
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
        Log.e(TAG, "value of value[0] = " + Boolean.toString(value[0]));
        return value[0];
    }
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Zowie Tay
  • 3
  • 2

0 Answers0