简体   繁体   English

如何将 MVVM 与 asyncTask 或任何其他东西一起使用?

[英]How to Use MVVM With asyncTask or any other thing?

我想使用 AsyncTaskLoader 但它已被弃用,所以我知道我可以使用 ViewModel 和实时数据或可变数据代替 AsyncTaskLoader,但我不知道如何将它们与异步任务一起使用。

First off, I would recommend using JobScheduler/WorkManager as Google states here .首先,我建议使用 JobScheduler/WorkManager 正如 Google在此处声明的那样

However, if you are still interested in using AsyncTask/AsyncTaskLoader , something like this might help.但是,如果您仍然对使用AsyncTask/AsyncTaskLoader感兴趣,这样的事情可能会有所帮助。 Since ViewModel holds a reference to a LiveData and the ViewModel updates the View (Activity or Fragment) , you can make a background network call(using AsyncTaskLoader) and update the liveData when onLoadFinished is called.由于ViewModel持有对LiveData的引用并且ViewModel更新View (Activity or Fragment) ,您可以进行后台网络调用(使用 AsyncTaskLoader)并在调用onLoadFinished时更新 liveData。 This update of LiveData should trigger the observable and eventually the View ( Activity/Fragment ) LiveData 的此更新应触发 observable 并最终触发 View ( Activity/Fragment )

Note: Make sure that the data you get back from the API call (For eg: <POJO.class> ) is of type MutableLiveData / LiveData .注意:确保您从 API 调用返回的数据(例如: <POJO.class> )是MutableLiveData / LiveData类型。

LiveData with ViewModel is still latest, and you can use simple AsyncTask with Android Architecture Components (LiveData and ViewModel) in order to make AsyncTask lifecycle aware. LiveData 与 ViewModel 仍然是最新的,您可以将简单的 AsyncTask 与 Android 架构组件(LiveData 和 ViewModel)一起使用,以使 AsyncTask 生命周期感知。 Loader is not as efficient as this method of doing background task. Loader 不如这种执行后台任务的方法有效。 Since you already know how to write AsyncTask, you only need it wrapped with LiveData and ViewModel: it works like a magic.由于您已经知道如何编写 AsyncTask,因此您只需要将它与 LiveData 和 ViewModel 一起包装:它就像魔术一样工作。 For information on using AsyncTask with LiveData and ViewModel, you can look it up at https://medium.com/androiddevelopers/lifecycle-aware-data-loading-with-android-architecture-components-f95484159de4有关将 AsyncTask 与 LiveData 和 ViewModel 一起使用的信息,您可以在https://medium.com/androiddevelopers/lifecycle-aware-data-loading-with-android-architecture-components-f95484159de4 中查找

Using AsyncTask with ViewModel defeats the purpose of using ViewModel.将 AsyncTask 与 ViewModel 一起使用违背了使用 ViewModel 的目的。

ViewModel basic use case is: ViewModel基本用例是:

public class MyViewModel extends ViewModel {
    private MutableLiveData<List<User>> users;
    public LiveData<List<User>> getUsers() {
        if (users == null) {
            users = new MutableLiveData<List<User>>();
            loadUsers();
        }
        return users;
    }

    private void loadUsers() {
        // Do an asynchronous operation to fetch users.
    }
}

In the loadUsers you are suppose to do the job - in background thread.loadUsers您应该在后台线程中完成这项工作。 You could use AsyncTask here but it makes no sense as you would not benefit from its doInBackground() and onPostExecute() methods.你可以在这里使用 AsyncTask ,但它没有意义,因为你不会从它的doInBackground()onPostExecute()方法中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM