Coova
Coova

Reputation: 1858

How to delay processes until and AsyncTask finishes?

I am running into an issue where an object is being created and sent to the back-end (PARSE) with variables that are "null" because another AsyncTask has yet to be completed.

In this current situation, I retrieve the co-ordinates of a location and than pass them back from the AsyncTask to the Fragment via and interface. Within this interface method I am assigning longitude and latitude values to a global variable.

These newSportEvent.saveInBackground(); seems be to called before the _longitude/_latitude variables are given a value from the AsyncTask. I can wrap the .saveInBackground() in an if statement but that still leaves me with an issue that involves pressing the "accept" button twice for everything to finalize.

How can I delay the process to not create the Object and send it to the DB until the values are set?

    case R.id.btnAccept:
        String desc = etDesc.getText().toString().trim();
        String loc = etLocation.getText().toString().trim();
        String time = btnTime.getText().toString().trim();
        String date = btnDate.getText().toString().trim();
        String sport = tvSportName.getText().toString().trim();
        String title = etTitle.getText().toString().trim();
        ParseUser currentUser = ParseUser.getCurrentUser();

        // check to see if the values are empty

        SportEvent newSportEvent = new SportEvent();
        newSportEvent.setSport(sport);
        newSportEvent.setTime(title);
        newSportEvent.setDate(date);
        newSportEvent.setTitle(title);
        newSportEvent.setDesc(desc);
        newSportEvent.setUser(currentUser);
        Log.e("LONGITUDE11111", "" + _longitude);
        Log.e("LATITUDE111111", "" + _latitude);
        //newSportEvent.setLatitude(_latitude);
        //newSportEvent.setLongitude(_longitude);

        // getLatLongFromAddress(loc);
        new LocationAsyncTask(this).execute(loc);

        // implement user signin first>>>

        // newSportEvent.setUser(ParseUser.getCurrentUser());
        ParseACL acl = new ParseACL();
        acl.setPublicReadAccess(true);
        acl.setPublicWriteAccess(true); // objects created are writable
        newSportEvent.setACL(acl);

        // create dialog

        if (!(_longitude == null) || !(_latitude == null)) {
            // publish to ParseDB
            Log.e("LONGITUDE11111", "" + _longitude);
            Log.e("LATITUDE111111", "" + _latitude);
            newSportEvent.setLatitude(_latitude);
            newSportEvent.setLongitude(_longitude);
            newSportEvent.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    // Update the display
                    changeFragment();
                    // progressDialog.dismiss();
                }
            });
        }

        break;

    case R.id.btnCancel:
        changeFragment();
        break;
    }

}

private void showNavigationActivity() {
    Log.e("BUTTON", "SIGN UP PRESSED");
    Intent intent = new Intent(getActivity(), NaviActivity.class);
    startActivity(intent);
}

@Override
public void getLocation(ArrayList<String> string_array) {
    // TO-DO Auto-generated method stub
    _longitude = string_array.get(0);
    Log.e("LONGITUDE", _longitude);
    _latitude = string_array.get(1);
    Log.e("LATITUDE", _latitude);
}

Upvotes: 0

Views: 112

Answers (1)

Jeffrey Mixon
Jeffrey Mixon

Reputation: 13616

Move the newSportEvent.saveInBackground() and related code to the onPostExecute() method in your AsyncTask. This will ensure this code will only get executed after the AsyncTask has completed.

Upvotes: 2

Related Questions