簡體   English   中英

如何避免在AsyncTask中傳遞太多輸入參數?

[英]How can I avoid passing too many input parameters in AsyncTask?

我有一個AsyncTask ,我需要將多個參數傳遞給構造函數。 我知道它的壞習慣是傳遞過多的參數,並且最好將方法拆分成較小的塊以避免這種情況,但是我不允許將方法拆分,所以我唯一的方法是找到一種更好的方法來傳遞參數。 在我的AsyncTask ,我創建了一個構造函數。

是否可以使用值對象創建模型類並傳遞它們? 我是否需要吸氣劑和吸氣劑?

 class UpdateAsyncTask extends AsyncTask<Void, Void, Void> {

        private String productId;
        int mutationAmount;
        boolean isOpeningStock;
        FlightLegIdentifier fli;
        String crew;
        String tabletId;

        UpdateAsyncTask(final String productId, final int mutationAmount, final boolean isOpeningStock,
                         final String flightKey,
                         final FlightLegIdentifier fli,
                         final String crew,
                         final String tabletId,
                         final AirFiApplication airFiApplication) {

            this.productId = productId;
            this.mutationAmount = mutationAmount;
            this.isOpeningStock = isOpeningStock;
            this.fli = fli;
            this.tabletId = tabletId;
            this.crew = crew;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            productStockAdapter.notifyDataSetChanged();
        }

        @Override
        protected Void doInBackground(Void... params) {
            StockUtils.saveMutation(productId, mutationAmount, isOpeningStock, flightKey, fli, crew,
            tabletId, getAirFiApplication());
            return null;
        }
    }

您可以創建一個模型類,並將其作為參數傳遞給AsyncTask

這是一個例子:

 private static class ModelClass {
    int length;
    int height;

    ModelClass(int l, int h) {
        this.length = l;
        this.height = h;
    }
}

定義AsyncTask任務

 private class MyTask extends AsyncTask <ModelClass, Void, Void> {


    @Override
     protected void onPreExecute(){

        }

    @Override
    protected void doInBackground(ModelClass... params) {
        int length = params[0].length;
        int height = params[0].height;
        ...
        //here u can perform your saveMutation() function using the parameters...
    }

    @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
}

使用Aysnctask

 //Initialize model class
ModelClass params = new ModelClass(10,10);

//pass it to asynch tash
MyTask myTask = new MyTask();
myTask.execute(params);

您可以創建一個類,定義要傳遞給AsyncTask的參數。

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@AllArgsConstructor(suppressConstructorProperties=true)
@Getter
@Setter
class Product {
  private String productId; 
  private int mutationAmount; 
  private boolean isOpeningStock;
  private String flightKey;
  private FlightLegIdentifier fli;
  private String crew;
  private String tabletId;
  private AirFiApplication airFiApplication;
} 

class UpdateAsyncTask extends AsyncTask<Product, Void, Void> {
  ...
  ...
  @Override
  protected Void doInBackground(Product... params) {
  }
}

我將Project Lombok用於注釋( https://projectlombok.org/ )。 您必須將以下內容添加到build.gradle

dependencies { ... compile 'org.projectlombok:lombok:1.12.6' }

注意:您也可以對AsyncTask返回的值執行相同的AsyncTask

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM