【问题标题】:Is it possible to chain async tasks in Android?是否可以在 Android 中链接异步任务?
【发布时间】:2019-08-03 03:17:39
【问题描述】:

我一直在从事我的个人项目,以制作一个允许简单的锻炼追踪器的应用程序。我以前使用文件 I/O 执行所有操作,但从那以后尝试转向数据库,因为我认为它会更优雅。

我正在使用基本 repo 模式的 Android 应用程序上实现 Room 持久库:Fragment->ViewModel->Repository->DAO。

目前,我对数据库的查询非常简单。我只是想获得一个概念证明,看看我想如何与数据库交互。我有一种方法可以获取用户正在进行的当前锻炼,然后有一种方法可以获取与该锻炼相关的所有锻炼。我见过的大多数教程都使用 LiveData 对象,但我觉得这对我来说太过分了,因为一旦我获得数据,我就不再需要从数据库中进行任何更新,因此实时更新只是对性能的必要负担。当我使用 LiveData 对象时,我也遇到了类似的问题。

我有一个想法,让两个异步任务在我的片段中的“链”中运行,其中 UI 实际更新。第一个异步任务获取当前锻炼,完成后开始下一个任务以获取该锻炼的锻炼。

我发现有时当前的锻炼实际上并没有从视图模型中获取。通过调试,我发现查询不是问题。在视图模型类中,值始终存在。片段中的异步任务似乎是一个问题。

此外,每当碰巧找到当前锻炼,然后开始填充锻炼的下一步,我就会开始在返回的锻炼中找到间隙。随着从数据库返回的大型列表,我开始在索引 300 左右开始看到列表中的空白(下面的错误消息)。

我有一种感觉,我的下面的代码可能很幼稚,它并没有像我认为的那样做。我认为异步任务中的 doInBackground 将需要执行异步 Room 数据库查询所需的时间,然后一旦完成,它将调用 onPostExecute() ,这将在链中进一步推进。

如有必要,我可以共享来自数据库类的代码,但我不想被太多代码淹没,特别是因为视图模型类和存储库中的结果总是很好。问题似乎只出在片段中的代码上。

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_workout,container,false);
        entities = new ArrayList<>();
        /*
            Set up the view models
         */
        logModel = ViewModelProviders.of(getActivity()).get(LogViewModel.class);
        workoutModel = ViewModelProviders.of(getActivity()).get(WorkoutViewModel.class);

        GetCurrentWorkoutTask task = new GetCurrentWorkoutTask();
        task.execute();
        return view;
    }

    private class GetCurrentWorkoutTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... voids) {
            // get the current workout from the database
            logModel.getCurrentWorkout(); // view model then calls an async method to query the database
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if(logModel.getCurrentWorkoutResult()!=null) {
                // database found a workout, so assign it then move to the next stop in the chain
                currentWorkout = logModel.getCurrentWorkoutResult();
                Log.d("TAG","CurrentWorkout: "+currentWorkout);
                getExercises();
            }
            else{
                // no workout found,error
                Log.d("TAG","Get current workout result was null!");
            }
        }
    }

    public void getExercises(){
        GetExercisesTask task = new GetExercisesTask();
        task.execute();
    }

    private class GetExercisesTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... voids) {
            // get the exercises from the database
            workoutModel.getExercises(currentWorkout); // view model then calls an async method to query the database
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if(workoutModel.getExercisesResult()!=null){
                // all of the exercises have been loaded into the view model's list member variable since the query is complete
                printExercises(workoutModel.getExercisesResult());
            }
            else{
                Log.d("TAG","Get exercises result was null!");
            }
        }
    }

    public void printExercises(ArrayList<WorkoutEntity> rawData){
        Log.d("TAG","Rawdata size: "+rawData.size());
        int count = 0;
        for(WorkoutEntity entity : rawData){
            Log.d("TAG","Entity is: "+entity.toString());
            count++;
        }
        Log.d("TAG","Count is: "+count);
    }     

现在,我只是使用 for 循环将 1000 条记录插入数据库,并将日期指定为计数器。大小始终为 1000,但此处显示它永远不会达到第 1000 天,因为这是它从 printExercises 方法停止的地方。

天:807

天:821

天:822

天:823

看看它是如何从 807 跳到 821 的?每次也不一样,这就是为什么我确定我只是弄错了异步任务的功能,但找不到任何东西来回答我的问题。此外,在 printExercises() 方法中循环遍历结果时应该打印计数的日志消息永远不会被调用。

我有一个理论,可能是因为我在 doInBackground 中返回 null 并且我需要直接从 viewModel 方法返回一些内容,以便任务知道异步调用已完成。我明天早上会尝试这样做,但我想发布这个以防其他人可以给我任何见解,因为我已经盯着这个看了几个小时并且没有得到任何结果。

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    在仔细研究之后,它似乎实际上只是 logcat 的一个问题。我通过应用程序进行了调试,发现列表是完整的,没有间隙,所以我猜一次发生这么多 logcat 会导致一些奇怪的行为。

    【讨论】:

      猜你喜欢
      • 2012-06-21
      • 2016-01-02
      • 2016-04-24
      • 2014-08-06
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2013-07-25
      • 2011-11-21
      相关资源
      最近更新 更多