MVVM-在关闭应用程序时从通知开始访问BroadcastReceiver中的ViewModel/SQLite [英] MVVM - Accessing ViewModel/SQLite in a BroadcastReceiver started from a notification when app is closed

查看:228
本文介绍了MVVM-在关闭应用程序时从通知开始访问BroadcastReceiver中的ViewModel/SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有每隔几天发送一次的提醒通知.

I have a reminder notification that is sent every few days.

通过重复的 AlarmManager 触发该通知的发送.通知本身建立在我的 BroadcastReceiver onReceive 中(如此处).因此,当触发 onReceive 时,该应用程序甚至无法打开/运行.

The sending of that notification is triggered with a repeating AlarmManager. The notification itself is built in the onReceive of my BroadcastReceiver (as described here). So when onReceive is triggered, the app is not even open/running.

现在,此时我想访问我的(本地)SQLite数据库,并获取用于构建通知的正确内容,但是我将如何获取 ViewModelProvider (代码中的xxx),甚至可以访问我的 ViewModel ?

Now at that point I want to access my (local) SQLite database and get the correct content to build the notification with, but how would I get a ViewModelProvider (xxx in the code) in this place to even access my ViewModel?

public void onReceive(Context context, Intent intent) {    

    NotificationViewModel viewModel = 
    ViewModelProviders.of(XXX).get(NotificationViewModel.class);

    //do stuff
}

或者问一个更好的问题,那是即使好的做法吗?
另一种可能性是将所有内容填充到 PendingIntent 中,这将触发 onReceive ,因此一旦收到,我就可以一次一个地检索它.但这会更加困难,因为它是重复的警报,每次都需要不同的内容,但仅触发一次.

Or to ask the better question, is that even good practice?
The other possibility would be stuffing all the content in the PendingIntent that will trigger the onReceive, so I could retrieve it one by one once received. But that would be even harder, since it's a repeating alarm and needs different content every time but is triggered only once.

我查看了一些搜索结果,但它们没有解决我的问题:

I've looked at a few search results but they did not solve my issue:

  • 用于自定义的MVVM架构Android上的观看次数
    ->似乎有很多代码可以解决这个小问题,加上Kotlin,这对我来说很难理解
  • 在Viewmodel中访问BroadCastReceiver
    -> 这似乎是我需要的最接近的,但它也需要在之前进行设置.我想尝试一下,但是我需要手动创建MainActivity来访问其变量.不会在没有警告的情况下在用户设备上打开新活动吗?
    在我的应用程序不在前台的情况下,甚至可以访问我的数据库吗?
  • MVVM Architecture for Custom Views on Android
    -> Seems like a lot of code for such a minor issue, plus being Kotlin, which is hard to understand for me
  • Dealing with BroadcastReceivers using the Model View Presenter design pattern
    -> Here it seems like it is necessary to set up a few things in the activities first, before even being able to use it, but I need to start this code without having my application running
  • Accessing BroadCastReceiver in Viewmodel
    -> This seems like the closest to what I need, but it also needs previous setting up before. I wanted to try this but I would need to manually create my MainActivity to access its variables like that. Wouldn't that open a new activity on the user's device without warning?
    Is it even possible to access my database without my app being in the foreground?

阅读超出LiveData就是说ViewModel [...]

如果您的应用程序的一部分不影响用户界面,则您可能不需要LiveData.

If part of your app doesn’t affect the UI, you probably don’t need LiveData.

所以这意味着我应该只使用上下文访问我的存储库,并从其中获取原始数据,没有LiveData包装器?

So that means I should simply access my repository with the context and get the raw data out of it, without the LiveData wrapper?

所以

public void onReceive(Context context, Intent intent) {

    NotificationRepository rp = new NotificationRepository(context);
    MessageNotification notification = rp.getNextNotification();
}

代替

public void onReceive(Context context, Intent intent) {

    NotificationViewModel viewModel = 
    ViewModelProviders.of(XXX).get(NotificationViewModel.class);
    MessageNotification notification = 
    viewModel.getNextNotification().observe(XXX, new 
         Observer<MessageNotification>() {
            @Override
            public void onChanged(MessageNotification messageNotification) {
                //do stuff
            }
         });
}

但这是否违反MVVM约定?
我应该使用其他架构吗?现在,这对我来说似乎很有意义,因为我只检索了一次,而不必观察更改.

But does this go against the MVVM convention?
Should I use some other architecture? Right now it seems to make sense to me, since it's something I only retrieve once and don't have to observe for changes.

推荐答案

在这种情况下ViewModel的真正目的是什么?
是否会将您的数据转换为便于查看的格式?
它将处理数据更新吗?(我的意思是,是否会进行数据更新?似乎您会在一段时间内收到一条通知)
还是只是将整洁,同步的代码弄杂而无实际目的地使其异步?

What is the real purpose of ViewModel in this situation?
Will it transform your data to some view-convenient format?
Will it handle data updates? (I mean, will there ever be data updates? Seems you have one notification in a while)
Or will it just clutter clean, synchronous code and make it async for no real purpose?

如果仅对最后一个问题回答是",则可能不需要此处的ViewModel :)您是否需要其他架构?不,您不需要架构.您需要显示一条通知,只需执行此操作即可!

If you answer 'yes' only to the last question, you probably do not need ViewModel here:) Do you need other architecture? No, you need no architecture. You need to display a notification, so just do it!

如果您是真正的MVVM粉丝,您仍然可以通过.
首先,删除 ViewModelProviders.of 导致无法在此处使用.它需要活动或碎片,而您却没有.ViewModelProvider的目的是在重新创建活动/片段时为您提供相同的ViewModel实例-显然不是您的情况.
其次,自己构造viewmodel: new NotificationViewModel().
第三,从您的视图模型而不是livedata返回普通对象,因为您的数据不是实时的.

If you are a real MVVM fan, you still can pass.
Firstly, drop ViewModelProviders.of cause it is impossible to use here. It requires activity or fragment and you have neither of them. Purpose of ViewModelProvider is to deliver you same instance of viewmodel when activity/fragment is recreated - it is clearly NOT your case.
Secondly, construct viewmodel yourself: new NotificationViewModel().
Thirdly, return the normal object from your viewmodel instead of livedata, cause your data is not live.

public class NotificationViewModel {
    MessageNotification getNextNotification() {
        // ...
    }
}

请注意,您甚至不需要扩展 ViewModel 类,因为您无需使用ViewModelProviders.

Note that you even do not need to extend ViewModel class, cause you do not use ViewModelProviders.

这篇关于MVVM-在关闭应用程序时从通知开始访问BroadcastReceiver中的ViewModel/SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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