AsyncTask的的get()方法:是否有任何场景,它实际上是最好的选择? [英] AsyncTask's get() method: Is there any scenario where it is actually the best option?

查看:169
本文介绍了AsyncTask的的get()方法:是否有任何场景,它实际上是最好的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答<一href="http://stackoverflow.com/questions/16905526/progressdialog-in-android-asynctask-does-not-display-at-the-right-time/16906914">this的问题,我有一个关于使用Android的的AsyncTask类的get()方法的意义/实用性毋庸置疑。

After answering this question, I got a doubt about the sense/usefulness of using the get() method of Android's AsyncTask class.

public final Result get ()

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

基本上,一个同步解决了AsyncTask的类,即块(冻结)的用户界面,直至背景操作完成。

Basically, a synchronous solution to the AsyncTask class, that blocks (freezes) the UI until the background operations are finished.

除了测试目的,即使在这种情况下,我真的不能相信任何场景,它实际上是一个很好的解决方案,但我可能是错的,让我感到好奇。

Other than test purposes, and even in those cases, I can't really think in any scenario where it is actually a good solution, but I may be wrong, so I feel curious.

如果您需要用户真正等到AsyncTask的完成,则可以显示一个对话框或ProgressDialog,其在每一个瞬间UI的控制。我知道这是不完全一样的,但恕我直言,这是一个比使用的get()方法更好的方法。

If you need the user to actually wait until the AsyncTask finishes, you can show a Dialog or ProgressDialog, having the control of the UI in every moment. I know it's not exactly the same, but IMHO it's a much better approach than using the get() method.

推荐答案

的AsyncTask 是做后台操作中不是唯一的方式。事实上,文件说,的AsyncTask 只能用于业务需要至多几秒钟。所以,如果你有需要较长时间的任务,他们应该是通过实现的 Runnable接口。其他(非AsyncTask的)线程这样的任务,可能需要等待的AsyncTask 来完成,所以在我看来,这个想法有没有情况下,人会愿意使用 AsyncTask.get()是假的。

AsyncTask isn't the only way of doing background operations. Indeed, the documentation says that AsyncTask should only be used for operations take at most a few seconds. So if you've got tasks that take longer, they should be coded via classes that implement the runnable interface. Such tasks in other (non AsyncTask) threads may well want to wait for an AsyncTask to finish, so it seems to me that the idea that there are no situations where one would want to use AsyncTask.get() is false.

更新:在回应评论,强调这可能是一个有效的使用 AsyncTask.get(),以下是可能的操作:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆