Aesgarth
Aesgarth

Reputation: 23

How can I make sure a function runs AFTER the function that calls it is complete?

I have a function that gets some weather data and outputs it to a text view in-app. I need to update another text view after this has happened (based on the weather data and some other variables...) and I have a function that performs this task, but it seems to run before the previous function is complete as I'm doing it now. The function that deals with the weather data is called weatherUpdate, the function that deals with the second text update is called textUpdate. I'm calling the textUpdate function right at the end of the weatherUpdate function...

How do I ensure that textUpdate runs after weatherUpdate is finished?

void weatherUpdate() {
    //Weather API url and key
    String apiURL = "https://api.openweathermap.org/data/2.5/weather?lat=00.0000&lon=00.0000&units=metric&APPID=00000000000000000000000000000000";
    //Request JSON data from weather API
    RequestQueue queue = Volley.newRequestQueue(this);
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiURL, null, new Response.Listener<JSONObject>() {
        //Parse data to get temperature
        @Override
        public void onResponse(JSONObject response) {
            try {
                //Get temperature data, format it to 1 decimal place, and output in the text box.
                temp1 = (response.getJSONObject("main").getDouble("temp"));
                String tempFormatted = (getString(R.string.temp_format, temp1));
                tempBox.setText(tempFormatted);
                //get the icon for weather conditions.
                String iconName = response.getJSONArray("weather").getJSONObject(0).getString("icon");
                String imageURL = String.format("http://openweathermap.org/img/w/%1s.png", iconName);
                Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
            } catch (JSONException e) {
                //catch errors and toast error message.
                e.printStackTrace();
                Toast errorToast = Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG);
                errorToast.show();
            }
        }
        //Request error handler
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast errorToast = Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG);
            errorToast.show();
        }
    });
    queue.add(request);
    //Toast notification that update has run.
    Toast.makeText(MainActivity.this, "Weather Updated!", Toast.LENGTH_SHORT).show();
    textUpdate(); //<-this is where my problem is. It seems to run before the above is finished.
}

Upvotes: 0

Views: 53

Answers (1)

Csaba Farkas
Csaba Farkas

Reputation: 882

I think you're calling textUpdate at the wrong place. You are executing an asynchronous network call and you should only call textUpdate in your callback function. See below - call the function in onResponse.

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, apiURL, null, new Response.Listener<JSONObject>() {
        //Parse data to get temperature
        @Override
        public void onResponse(JSONObject response) {
            try {
                //Get temperature data, format it to 1 decimal place, and output in the text box.
                temp1 = (response.getJSONObject("main").getDouble("temp"));
                String tempFormatted = (getString(R.string.temp_format, temp1));
                tempBox.setText(tempFormatted);
                //get the icon for weather conditions.
                String iconName = response.getJSONArray("weather").getJSONObject(0).getString("icon");
                String imageURL = String.format("http://openweathermap.org/img/w/%1s.png", iconName);
                Glide.with(MainActivity.this).load(imageURL).into(weatherImage);
                textUpdate();
            } catch (JSONException e) {
                //catch errors and toast error message.
                e.printStackTrace();
                Toast errorToast = Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG);
                errorToast.show();
            }
        }
        //Request error handler
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast errorToast = Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG);
            errorToast.show();
        }
    });

Upvotes: 2

Related Questions