Johan
Johan

Reputation: 35213

AsyncTask, handle result in onPostExecute vs get()

I would like to handle a return result from an AsyncTask outside of the class.

Is there any downside using, for example, Location loc = TheClass.execute().get();?
Should I handle the result in onPostExecute inside the class instead?

Upvotes: 2

Views: 1237

Answers (4)

Arun George
Arun George

Reputation: 18592

The only place where you can be assured that the operation you have started in doInBackground() has completed is the callback method onPostExecute(). So using a get() is not such a wise idea as far as AsyncTask is concerned.

Upvotes: 1

user370305
user370305

Reputation: 109247

get()

The purpose, of get(), is to block until the result is obtained. This could be useful, for example, if you have multiple tasks, where one uses another. One task could start another and call get() to wait for it to finish before continuing with its own work.

onPostExecute (Result result)

Runs on the UI thread after doInBackground(Params...). The specified result is the value returned by doInBackground(Params...).

This method won't be invoked if the task was cancelled.

Upvotes: 0

Gorets
Gorets

Reputation: 2442

The get() method is not well method for it, cause it stoped UI-thread

Upvotes: 2

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

get() make you (current Thread) wait until the result come and onPostExecute did work Asynchronously and work like a call back when the background work has been completed.

http://developer.android.com/reference/android/os/AsyncTask.html#get()

public final Result get ()

Waits if necessary for the computation to complete, and then retrieves its result.

Upvotes: 0

Related Questions