Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This method getBedHistory takes an input as list of beds called allBedsWithoutStatus() and a date. I have 34 beds in my allBedsWithoutStatus(). It calls a method called calculateStatus() and then calculates the status, and passes it back as an integer. The results used to create a new array allBedsWithStatus(). The new array allBedsWithStatus only contains the details of my input array (allBedsWithoutStatus)

Status = open = 0; allocated = 1; etc…

calculateStatus includes a firebase call which has an oncomplete listener which makes it an asynchronous method so I therefore added the callbackb which is intended to wait until the method completes before it returns with the status.

The calculateStatus method is rolling through the bedIds and getting the status at the date correctly.

However, on runtime this routine drops straight to bottom of method and returns 0 for every status

when I return to the calling method getBedHistory() (see below) The last element in my arraylist is showing 34 times and status of 0 for that bedId

Java
<pre>  public void getBedHistory() {
        int statusCode = 0;
        for (int u = 0; u < allBedsWithoutStatus.size(); u++) {
            bedIdString = allBedsWithoutStatus.get(u).getBedId();
            wardIdString = allBedsWithoutStatus.get(u).getWard();
            System.out.println("Before calculate This the bed id and ward id " + u + "  " + bedIdString + " " + wardIdString);


                 bedStatusAtDate  =  calculateStatus(bedIdString, new Callbackb() {
                @Override
                public void callb() {


                    System.out.println("This is the bed status at date  = " + BedStatusChartsForDate.this.bedStatusAtDate);

                    // Put as much as the logic we can here
                    // all i need is the status of each bed
                    switch (wardIdString) {
                        case "St Johns":
                            wardCode = 0;
                            break;
                        case "St Marys":
                            wardCode = 1;
                            break;
                        case "St Pauls":
                            wardCode = 2;
                            break;
                        case "St Magz":
                            wardCode = 3;
                            break;
                        case "St Joes":
                            wardCode = 4;
                            break;
                        default:
                            wardCode = 5;
                    }  // end Switch for Ward

                    bedStatus[0] = statusCode;
                    bedStatus[1] = wardCode;
                    bedDetails.setBedId(bedIdString);
                    bedDetails.setWard(wardId);
                    bedDetails.setStatus(statusCode);
                    BedStatusChartsForDate.this.allBedsWithStatus.add(bedDetails);
                    int size =  allBedsWithStatus.size() - 1;
                    System.out.println("List of all beds with status added end of getBedHistory = " + "  " + size + " " + allBedsWithStatus.get(size).getBedId() + allBedsWithStatus.get(size).getWard() + " " + allBedsWithStatus.get(size).getStatus());

                }
            });
        }
    }


// Method here to get all the beds from the beds collection to then to be passed in to get the bed history in the next method
    public int calculateStatus(String bedId, Callbackb callbackb) {
        System.out.println("This the bed Id before using db call "+ bedId);
        //   System.out.println("this should be details returned "+   .getAllBedDetails());
        //Now get the history of the bed to get the status
        db.collection("bed")
                .document(bedId)
                .collection("bedHistory4")
                .orderBy("dateAndTime")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task1) {
                        if (task1.isSuccessful()) {
                            String event = "";
                            String eventDateString = "";
                            Date eventDateFromDb = new Date();
                            Date dateSelectedFromScreen = new Date();
                            for (QueryDocumentSnapshot history : task1.getResult()) {
                                eventDateString = history.getString("dateAndTime");
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                                try {
                                    eventDateFromDb = format.parse(eventDateString);
                                    dateSelectedFromScreen = dateFormatter.parse(dateFromDateSelected);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                //   System.out.println("This is date time returned " + ": " + eventDateFromDb);
                                //   System.out.println("This is date time string  " + ": " + dateSelectedFromScreen);

                                //Storing the eventType for transactions before or equal to the selectedDate
                                if (eventDateFromDb.compareTo(dateSelectedFromScreen) <= 0) {
                                    event = history.getString("eventType");
                                }
                            }
                            // end For loop
                            // now use the event to set the status in the bedStatus array.
                            switch (event) {
                                case "Added bed":
                                case "Bed allocated to ward":
                                case "Bed is now open":
                                case "Bed is cleaned – ready for next patient":
                                    statusCode = 0;
                                    break;
                                case "Bed allocated - patient on way":
                                    statusCode = 1;
                                    break;
                                case "Patient in bed in ward":
                                    statusCode = 2;
                                    break;
                                case "Bed ready for cleaning":
                                    statusCode = 3;
                                    break;
                                default:
                                    statusCode = 4;
                                    System.out.println("This bed is the bed not created yet" + bedId);
                                    break;
                            }   // end Switch for bedStatus

                            System.out.println("Bed details added to all beds with status " + bedId +  " " + " " + statusCode);
                            callbackb.callb();
                        } else {
                            Log.d(TAG, "Error getting documents: ", task1.getException());
                        }

                    }
                });
        return statusCode;
    }


What I have tried:

Everything..any help at all would be greatly appreciated
Posted
Updated 5-Apr-21 4:59am
v3
Comments
David Crow 3-Apr-21 22:17pm    
Your code snippet seems to be missing at least two closing curly braces. Without those, it's hard to tell if the calculateStatus() function is contained inside or outside of getBedHistory(). Which function does statusCode belong to? It appears to be defined in getBedHistory() but it is being returned in calculateStatus(). That seems odd. Should calculateStatus() have its own copy?
Cormac Mattimoe 4-Apr-21 9:51am    
sorry David it is the way the code is formatted. calculateStatus() and getBedHistory() are both separate methods..status code belongs to calculateStatus() that's what that method should return when it finishes going through that method but it just jumps to bottom of method onComplete and returns 0
David Crow 4-Apr-21 21:51pm    
"...it is the way the code is formatted."

I can't tell if it is a formatting issue or an omission issue. If the latter, please update your post to include the missing curly braces.

"...status code belongs to calculateStatus()..."

From what you've shown, statusCode is defined in getBedHistory(), and since it looks as though calculateStatus() is also defined in getBedHistory(), that's why calculateStatus() would have access to it.

"...it just jumps to bottom of method onComplete and returns 0"

Is it returning 0 because that was the value it was defined with, or because that's the value it was assigned in the switch statement? Change it's default value to -1 to help differentiate.
Cormac Mattimoe 5-Apr-21 11:02am    
Hi David, I have updated the question to include the right formatting. Hope this all makes sense now.
"Is it returning 0 because that was the value it was defined with, or because that's the value it was assigned in the switch statement? Change it's default value to -1 to help differentiate." So I call the method in getBedHistory() and it enters calculateStatus() but jumps to bottom of return instead of going through code in calculateStatus and then returning to getBedHistory(), thanks very much for your helping

1 solution

We can't tell: without your data to work from we have no idea what is happening or why.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Cormac Mattimoe 3-Apr-21 10:31am    
Hi there,

Thank you very much for getting back to me. I have use the debugger all the time. The problem is the code is being executed before the code is in the onComplete listener so example. I have my method called calculateStatus(). Once this is called it the code is executed by the calling of the method and jumps straight down to the return where it is suppose to go into the code in the method execute that and then return the right answer from that code instead of returning 0 like it is now.

Hope this makes sense

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900