0

When logging within the if statements I can successfully get the value of the "startTime" variable. My issue arises as soon as I try and access this variable from outside of the if statement.

public class MainActivity extends AppCompatActivity {
String startTime = "";
String endTime = "";
....
....
....
public void dailyHours(){
    //Retrieve clockInId (ObjectID for clockInTime)
    ParseQuery<ParseObject> startQuery = ParseQuery.getQuery("clockIn");
    startQuery.getInBackground(clockInId, new GetCallback<ParseObject>() {
        public void done(ParseObject clockIn, ParseException e) {
            if (e == null) {
                startTime = clockIn.getString("Time");
                Log.d(TAG, "StartTime: " + startTime);
            } else {
                // something went wrong
            }
        }
    });

    //Retrieve clockOutId (ObjectID for clockOutTime)
    ParseQuery<ParseObject> endQuery = ParseQuery.getQuery("clockOut");
    endQuery.getInBackground(clockOutId, new GetCallback<ParseObject>() {
        public void done(ParseObject clockOut, ParseException e) {
            if (e == null) {
                endTime = clockOut.getString("Time");
                Log.d(TAG, "EndTime: " + endTime);
            } else {
                // something went wrong
            }
        }
    });

}

For example, when the Log.d() gets moved to outside of the ParseQuery, startTime returns back empty.

public void dailyHours(){
    //Retrieve clockInId (ObjectID for clockInTime)
    ParseQuery<ParseObject> startQuery = ParseQuery.getQuery("clockIn");
    startQuery.getInBackground(clockInId, new GetCallback<ParseObject>() {
        public void done(ParseObject clockIn, ParseException e) {
            if (e == null) {
                startTime = clockIn.getString("Time");
            } else {
                // something went wrong
            }
        }
    });
    Log.d(TAG, "StartTime: " + startTime); //startTime returns an empty string when moved here.

3 Answers 3

2

GetCallback<ParseObject>() method is called asynchronously (in another thread), so actually assignment startTime = clockIn.getString("Time") can be executed after Log.d(TAG, "StartTime: " + startTime).

You have "race condition" here.

Sign up to request clarification or add additional context in comments.

2 Comments

Essentially what I am trying to do is retrieve two objects from parse and then take the difference of the two. Can you guide me in the right direction in order to fix my issue? How can I ensure that startTime and endTime gets initiated first? That way I can go back and take the difference.
One of possible fixes is to add: Object timeLock = new Object(); to MainActivity, then check synchronized(timeLock) { if (startTime.length() > 0 && endTime.length() > 0) // compute difference } inside each if (e == null) block
2

in any case either if or else gets executed. And you are setting value in if section everytime. If your code goes in else block, it means if didnt execute which means value wasn't set. If you want to access that value in both if and else, set it before if block.

Comments

2

There is no issue with the scope of the variables, It is a issue of synchronization. Here you have a race condition. It is because if the getInBackground. They do not execute in the same thread.

And just after startQuery.getInBackground it spouns a new thread of exectuion. and straight come to the next line.

Log.d(TAG, "StartTime: " + startTime); //startTime returns an empty s

And there it is not guaranteed that the background is completed and executed the call back method before coming to the above line.

2 Comments

How can I gurantee that the background gets completed before the rest executes ?
Actually you should not. That is the whole point with the synchronization. The reason you start background threads is not to block the execution of the main thread of the application. If you block it you will get ANR error and the application crashes. That is why we have call back methods to handle/ execute anything on the main thread after the execution of the background task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.