Reputation: 2366
I've noticed on the documentation for AsyncTask here that you can use a method called get()
to retrieve your result once the work on the thread is done. The documentation says that it
Waits if necessary for the computation to complete, and then retrieves its result.
Does that mean if I have this line of code:
List<Data> data = someAsyncTask.execute.get();
in the main UI thread, does it wait for the task to complete before executing any code after it? If so, this would render the use of AsyncTask useless. What am I missing here?
Is AsyntTask.get()
an alternative to using onPostExecute()
to return data to the main thread? If so, is it safe? Or is its use for something completely different?
Upvotes: 3
Views: 384
Reputation: 4286
If you call AsyncTask.get()
and the task is not completed, then current thread will wait (and can be interrupted).
You right, calling this method in UI thread makes AsyncTask useless. But you can call it in another thread which need result of this task for further execution.
Upvotes: 2