user2083181
user2083181

Reputation: 17

ensuring an async thread runs on the ui thread

I have objects (not on the ui thread) that need to run the async task as internal methods and I want to pass the ui thread to the method and then implement the ui thread on the asynctask that way - is there a way of doing this?

pseudo-code:

      public class EditDetails extends Activity{          
                MyObject obj = new obj;
                obj.syncWithServer(EditDetails.this);
      }

      public class MyObject {
           public void syncWithWerver(EditDetails parent){
                 final class GetUserHttpTask extends AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> {
                        @Override
                        protected String doInBackground(String... params) {
                             return "";
                        }
                        @Override
                        protected String onPostExecute(String result) {
                              //this doesn't run
                        }
                 }
           }
      }          

note: at the moment this isn't working because onPostExecute isn't firing after the doInBackground

EDIT

this code is pseudocode the actual problem is still happening and I have listed it http://pastebin.com/jW6JpUbg here.. basically it gets stuck after running line 72 on the pastebin in never does onPostExecute

Upvotes: 0

Views: 118

Answers (2)

Boris Strandjev
Boris Strandjev

Reputation: 46963

Weird enough the onPostExecute should be void. I wonder how is your code compiling as you have changed the return type of overridden method?

After you correct your onPostExecute you can get the AsyncTask run as follows:

final class GetUserHttpTask extends AsyncTask<String/* Param */, Boolean /* Progress */, String /* Result */> {
        @Override
        protected String doInBackground(String... params) {
             return "";
        }
        @Override
        protected String onPostExecute(String result) {
              // this will run
        }
} 
public void syncWithWerver(EditDetails parent){
     AsyncTask task = new GetUserHttpTask();
     task.execute();
}

Upvotes: 2

Vishal Vyas
Vishal Vyas

Reputation: 2581

Did you tried executing your task like this ?

parent.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            new GetUserHttpTask().execute();
        }
});

I think this should work..

Added:

Your AsyncTask implementation should be as below : Note : Read the updated comments

class GetUserHttpTask extends AsyncTask<String/* in parameter for doInBackground */, Boolean /* in parameter for onProgressUpdate */, String /* Out parameter and in parameter for onPostExecute method*/> {
    @Override
    protected String doInBackground(String... params) {
        return "";
    }

    @Override
    protected void onPostExecute(String result) {
        // this doesn't run
    }

    @Override
    protected void onProgressUpdate(Boolean... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
}

Upvotes: 1

Related Questions