在Activity或Fragment之外获取ViewModel实例的正确方法 [英] The correct way to obtain a ViewModel instance outside of an Activity or a Fragment

查看:1787
本文介绍了在Activity或Fragment之外获取ViewModel实例的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个位置应用程序,该应用程序在MainActivity中显示Room数据库中的背景位置.我可以通过调用来获取ViewModel

I'm building a location app where I display background locations from a Room database in my MainActivity. I can get a ViewModel by calling

locationViewModel = ViewModelProviders.of(this).get(LocationViewModel.class);
locationViewModel.getLocations().observe(this, this);

当我通过BroadCastReceiver接收位置更新时,应将周期性的背景位置保存到Room数据库中.应该通过调用locationViewModel.getLocations().setValue()

Periodic background locations should be saved to the Room database when I receive location updates via a BroadCastReceiver. They should be saved by calling locationViewModel.getLocations().setValue()

public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {

    static final String ACTION_PROCESS_UPDATES =
            "com.google.android.gms.location.sample.backgroundlocationupdates.action" +
                    ".PROCESS_UPDATES";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_PROCESS_UPDATES.equals(action)) {
                LocationResult result = LocationResult.extractResult(intent);
                if (result != null) {
                    List<Location> locations = result.getLocations();
                    List<SavedLocation> locationsToSave = covertToSavedLocations(locations)
                    //Need an instance of LocationViewModel to call locationViewModel.getLocations().setValue(locationsToSave)
                }
            }
        }
    }
}

问题是如何在像BroadcastReceiver这样的非活动类中获取LocationViewModel实例?调用locationViewModel = ViewModelProviders.of(context).get(LocationViewModel.class)是否正确,其中context是我从BroadcastReceiver的onReceive (Context context, Intent intent)接收的上下文?

Question is how should I get the LocationViewModel instance in a non-activity class like this BroadcastReceiver? Is it correct to call locationViewModel = ViewModelProviders.of(context).get(LocationViewModel.class) where context is the context that I receive from onReceive (Context context, Intent intent) of the BroadcastReceiver?

获取ViewModel之后,是否需要使用 LiveData.observeForever

After getting the ViewModel, do I need to use LiveData.observeForever and LiveData.removeObserver since the BroadcastReceiver is not a LifecycleOwner?

推荐答案

问题是我应该如何在 这样的非活动类,如BroadcastReceiver?

Question is how should I get the LocationViewModel instance in a non-activity class like this BroadcastReceiver?

您不应该那样做.它的设计不当.

You shouldn't do that. Its bad design practice.

调用locationViewModel =是否正确? ViewModelProviders.of(context).get(LocationViewModel.class)其中 上下文是我从onReceive接收到的上下文(上下文上下文, Intent intent)的广播接收器?

Is it correct to call locationViewModel = ViewModelProviders.of(context).get(LocationViewModel.class) where context is the context that I receive from onReceive (Context context, Intent intent) of the BroadcastReceiver?

不.它没有帮助

您可以按以下步骤实现所需的结果:

在单独的单例类中将您的Room DB操作与ViewModel分开.在ViewModel和任何其他需要的位置中使用它.接收到广播后,通过此单例类(而不是ViewModel)将数据写入DB.

Separate your Room DB operation from ViewModel in a separate singleton class. Use it in ViewModel and any other place required. When Broadcast is received, write data to DB through this singleton class rather than ViewModel.

如果您正在查看片段中的LiveData,那么它也会更新您的视图.

If you are observing for the LiveData in your Fragment, then it will update your views too.

这篇关于在Activity或Fragment之外获取ViewModel实例的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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