AsyncTask的目的可变参数参数 [英] Purpose of AsyncTask varargs parameters

查看:95
本文介绍了AsyncTask的目的可变参数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是谷歌使用可变参数的参数中的 的AsyncTask ?例如该方法<一href="http://developer.android.com/reference/android/os/AsyncTask.html#execute%28Params...%29"><$c$c>execute(), <一href="http://developer.android.com/reference/android/os/AsyncTask.html#doInBackground%28Params...%29"><$c$c>doInBackground()和<一href="http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress%28Progress...%29"><$c$c>publishProgress()所有使用 [类型] ... 符号。

What are the reasons that Google use varargs for the parameters in the AsyncTask? For example the methods execute(), doInBackground() and publishProgress() all use the [Type]... notation.

我认为,它是难使用,因此他们必须有一些,我忽略了很好的理由?

I think that makes it "harder" to use so they must have some good reasons which I overlooked?

所以,无论我们有没有参数,一个或多个参数。让我们把它分解:

So, either we have no parameters, one or many parameters. Let's break it down:

  1. 无参数(简单): PARAMS 参数虚空和而已。 (该方法不能使用它......所以这是pretty的安全。)

  1. No parameters (easy): Params parameter is Void and that's it. (The methods cannot use it... so that's pretty safe.)

一个参数:在这里,我至少觉得有必要做检查之初的 doInBackground()方法。例如,下面是一个任务接收整数和生产类型的结果双击

One parameter: Here, I at least, feel the need to make a check at the beginning of the doInBackground() method. For example, here is a task receiving an Integer and producing a result of type Double:

public Double doInBackground(Integer... myParameters) {
    // we are only expecting one parameter
    if (myParameters.length != 1)
        throw new IllegalArgumentException("!= 1");

    return 100d * myParameters[0];
}

  • 多个参数。现在,这里一定是哪里谷歌做出了正确的选择呢?但是,当我看到它要么你有兴趣的的相同类型的参数列表的,或者你想要的不同类型的参数。谷歌只涉及这些案件之一(与不同类型的,你需要某种通用接口,在很多情况下,我结束了对象... 那是不是真的类型安全...)

  • More than one parameter. Now here must be where Google made the right choice? But as I see it's either you are interested in a list of parameters of the same type, or you want different types of parameters. Google only addressed one of these cases (with different types you need some kind of common interface. In many cases I end up with Object... and that isn't really type safe...)

    那么,是什么问题,如果我们只是删除可变参数干脆?下面是该方法的一个子集:


    So, what is the problem if we just remove the varargs altogether? Here's a subset of the methods:

    class AsyncTask<Param, Progress, Result> {
    
        abstract Result doInBackground(Param param);
        void publishProgress(Progress progress) { ... }
    }
    

    这将工作高于一切的情况。举例来说,如果我们要处理一组参数,我们可以只使用一个数组类型参数

    This would work for all the cases above. For example, if we want to handle an array of parameters we could just use an array type param:

    class MyAsyncTask extends AsyncTask<String[], Integer, String> { 
    
        String doInBackground(String[] param) {
            return Arrays.toString(param);
        }
    }
    

    我不明白的时候它可能是任何实际用途。但我敢肯定,我失去了一些东西教廷,我需要了解。 :)

    I don't see when it could be of any practical use. But I'm sure I'm missing something curia that I need to know about. :)

    推荐答案

    我觉得可变参数参数,就可以更加方便一点,当你的调用执行的的的AsyncTask

    I think the vararg arguments just makes it a bit more convenient when you call to execute the AsyncTask.

    只要我们想知道为什么AsyncTask的设计事情是这样的: - )

    As long as we are wondering why AsyncTask was designed the way it is: :-)

    在我看来,在参数结果模板不会是真的有必要来完成相同的。

    In my opinion, the Param and Result templates would not have been really necessary to accomplish the same.

    当你写你自己的的AsyncTask ,你继承它。而不是声明的实际类型参数的结果,你不妨加入最后的字段子类(PARAMS)并添加修改字段子类(结果)。例如:

    When you write your own AsyncTask, you subclass it. Instead of declaring the actual types for Param and Result, you may as well add final fields to your subclass (Params) and add modifiable fields to your subclass (Result). E.g:

    public class MyAsyncTask extends AsyncTask<Void> {
        // Input Params
        private final int inParam1;
        private final String inParam2;
    
        // Output Results
        private Bitmap resultBitmap;
    
        public MyAsyncTask(int param1, String param2) {
            inParam1 = param1;
            inParam2 = param2;
        }
    
        @Override
        protected void doInBackground() {
            // use param1 and param2 as input to the background process.
            ...
            ...
            // Finished: assign to result
            resultBitmap = ....;
        } 
    
        @Override
        protected void onPostExecute() {
            // Send result to UI.
            ... resultBitmap ...
            ...
            resultBitmap = null;
        }
    }
    

    没有仿制药需要时,也许除了显示进度。

    No generics needed, maybe except for showing Progress.

    这是我最常做的工作,特别是如果结果是位图。通过 onPostExecute doInBackground 返回和处理的值未设置为空后,所有设置和完成,其鬼祟 (由于通过保存在内存中的位图的内存错误完成/成品 AsyncTasks )泄漏位图这种方式。

    This is what i usually do anyway, especially if the result is a Bitmap. The value returned by doInBackground and handled by onPostExecute is not set to null after all is set and done and it sneakily 'leaks' Bitmaps this way (memory errors caused by bitmaps held in memory by completed/finished AsyncTasks).

    这篇关于AsyncTask的目的可变参数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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