So I am currently developing a date android application, now what I have done here is created a simple algorithm called similarDatesFunction
this function is responsible for retrieveing data and matching it with the user input, for every match a counter will be placed, if this exceeds 6 counters also known as the offPeak
variable it will set a false boolean value on my CalendarValidation.setDateFlag();
setter. Now I call/get this setter into another function called verifyValidations
where I check whether the object is carrying a false
or true
value. Now this works, but there’s a problem which I am unable to figure out.
When I run the android application, and input details it will find a match of 3 users within the same date according to my firebase database and return false (this is fine), however when I change the dates and then press on the button I get the same message which is that it cannot book and its still set to false, but if i press it again, it books the holiday (knowing that the date is new and is not already in the database) and is now set to true, this also happens vice versa. Essentially the boolean value is not resetting, rather it is carrying on with the next button click?
My code:
public void verifyValidations() { //boolean sameDate = CalendarValidation.isSameDateFlag(); CalendarInfo ci = new CalendarInfo(); //store start dates fields individually String startYearData = ci.getStartDateYear(); String startMonthData = ci.getStartDateMonth(); String startDayData = ci.getStartDateDay(); String startWeekData = ci.getStartDateWeek(); String totalDateCode = startYearData + startMonthData + startDayData + startWeekData; //store end dates fields individually String endYearData = ci.getEndDateYear(); String endMonthData = ci.getEndDateMonth(); String endDayData = ci.getEndDateDay(); String endWeekData = ci.getEndDateWeek(); String endTotalDateCode = endYearData + endMonthData + endDayData + endWeekData; String fullDate = totalDateCode + endTotalDateCode; sameDatesFunction(totalDateCode,endTotalDateCode); if (!CalendarValidation.isSameDateFlag()){ displayText.setText("No slots between these dates, choose another."); } else if (CalendarValidation.isSameDateFlag()){ displayText.setText("Holiday is available, booking successful."); } } public void sameDatesFunction (final String startCode, final String endCode) { final int offPeak = 6; FirebaseFirestore db = FirebaseFirestore.getInstance(); final Query query = db.collection("holidays").whereEqualTo("startDateReference",startCode); query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { @Override public void onSuccess(QuerySnapshot queryDocumentSnapshots) { //creating a map to obtain information Map<String, Object> test = new HashMap<>(); //counter int similarStartEndCounter = 0; int userNameCounter = 0; boolean flag = false; //retrieve data as a hashmap document for (QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){ Note data = documentSnapshot.toObject(Note.class); test.put("startDateReference", data.getStartDateReference()); test.put("endDateReference", data.getEndDateReference()); Collection<Object> values = test.values(); //retrieve results as single values for (Object string : values) { if ((string.equals(startCode)) || (string.equals(endCode)) ) { similarStartEndCounter++; } } } if (similarStartEndCounter >= offPeak) { CalendarValidation.setSameDateFlag(false); System.out.println("I am in sameDates as false"); } else { System.out.println("I am in sameDates as true"); CalendarValidation.setSameDateFlag(true); } } }); }
CalendarValidation.java
public class CalendarValidation { public static boolean sameDateFlag; public static boolean isSameDateFlag() { return sameDateFlag; } public static void setSameDateFlag(boolean sameDateFlag) { CalendarValidation.sameDateFlag = sameDateFlag; } public static boolean isSimilarFutureFlag() { return similarFutureFlag; } public static void setSimilarFutureFlag(boolean similarFutureFlag) { CalendarValidation.similarFutureFlag = similarFutureFlag; } public static boolean isSimilarPastFlag() { return similarPastFlag; } public static void setSimilarPastFlag(boolean similarPastFlag) { CalendarValidation.similarPastFlag = similarPastFlag; } public static boolean similarFutureFlag; public static boolean similarPastFlag; }
Advertisement
Answer
Posting this as a Community Wiki as it’s based on the comments provided by @Tom.
The problem is that you are performing an action to check the status of sameDateFlag
before the thread that waits for the database result to update it, and when you click on it again the thread will be completed, which is why it works by then.
I recommend you take a look at this community question for how to properly handle this synchronous calls to Firestore with the use of OnSuccessListener