在 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 是我从 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 和 LiveData.removeObserver 因为 BroadcastReceiver 不是 LifecycleOwner?
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 收到的上下文(上下文上下文,意图意图)的广播接收器?
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
和任何其他需要的地方使用它.当接收到 Broadcast 时,通过这个单例类而不是 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
.
如果您正在观察 Fragment 中的 LiveData
,那么它也会更新您的视图.
If you are observing for the LiveData
in your Fragment, then it will update your views too.