本文介绍了Android ViewModel:我应该“借用"吗?像官方示例一样来自LiveData的watch()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ViewModels时,View会观察ViewModel.它必须注册为观察员.在Google的官方教程中,此注册委托给 LiveData对象的方法.

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

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

public class MyActivity extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        // Create a ViewModel the first time the system calls an activity's onCreate() method.
        // Re-created activities receive the same MyViewModel instance created by the first activity.

        MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
        model.getUsers().observe(this, users -> {
            // update UI
        });
    }
}

方法getUsers()返回LiveData对象本身.它的observe()方法用于注册观察者.视图不遵循ViewModel,而是实现的一部分.

当与ViewModels合作时,不是观察自己而是以LiveData对象形式实现其部分实现,这是最佳实践吗?还是这是低质量的介绍?

解决方案

我想说的是,最佳做法是ViewModel通过某种形式的Observable(无论是LiveData还是RX Observable)公开其数据. /p>

这与其他架构(例如MVP)不同,在该架构中,主持人通常会引用对View的引用,当发生更改时会调用该View.这些准则对于ViewModel应该引用的内容非常具体.

A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

通过ViewModel将数据公开为Observable,这意味着视图可以来去去,一旦订阅,它将接收最新数据和任何后续更新.准则再次有一些细节.

If the activity is re-created, it receives the same MyViewModel instance that was created by the first activity. When the owner activity is finished, the framework calls the ViewModel objects's onCleared() method so that it can clean up resources

https://developer.android.com/topic/libraries/architecture /viewmodel.html

When working with ViewModels the View observes the ViewModel. It has to register as an observer. In the official tutorial of Google this registration is delegated to the observe() method of a LiveData object.

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

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

public class MyActivity extends AppCompatActivity {
    public void onCreate(Bundle savedInstanceState) {
        // Create a ViewModel the first time the system calls an activity's onCreate() method.
        // Re-created activities receive the same MyViewModel instance created by the first activity.

        MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
        model.getUsers().observe(this, users -> {
            // update UI
        });
    }
}

The method getUsers() returns the LiveData object itself. It's observe() method is used to register the observer. The View does not observe the the ViewModel but a part of it's implementation.

Now is this best practice, when working with ViewModels not to observe themselves but parts of their implementation in form of LiveData objects? Or is this an introduction of low quality?

解决方案

I'd say yes it's best practice for the ViewModel to expose its data through some form of Observable, whether that be LiveData or something like an RX Observable.

This breaks from other architectures such as MVP where the presenter would typically have a reference to the View which gets called when something changes. The guidelines are quite specific about what a ViewModel should reference.

A ViewModel must never reference a view, Lifecycle, or any class that may hold a reference to the activity context.

By exposing your data as an Observable through the ViewModel this means views can come and go, and once subscribed will receive the latest data and any subsequent updates. Again the guidelines have some detail.

If the activity is re-created, it receives the same MyViewModel instance that was created by the first activity. When the owner activity is finished, the framework calls the ViewModel objects's onCleared() method so that it can clean up resources

https://developer.android.com/topic/libraries/architecture/viewmodel.html

这篇关于Android ViewModel:我应该“借用"吗?像官方示例一样来自LiveData的watch()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:52