Android体系结构组件ViewModel-与Service/IntentService的通信 [英] Android Architecture Components ViewModel - communication with Service/IntentService

查看:1202
本文介绍了Android体系结构组件ViewModel-与Service/IntentService的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索Google的 Android体系结构组件.在我的项目中,我依靠服务 ViewModel 进行通信的正确方法是什么? ?使用LiveData可以实现吗?

I'm exploring Google's Android Architecture Components. In my project I'm relying on Services and IntentServices. What is the correct way to communicate with app's ViewModel from an IntentService or Service? Is it achievable using LiveData?

推荐答案

TL; DR可以实现-使用观察者关系.您的IntentService和可能的位置服务应该了解您的ViewModel.考虑使用存储库.可以使用LiveData(请参见 postValue ).更新UI(从ViewModel到Activity的通信)非常好,因为它具有生命周期意识.当您不更新UI时,可以考虑使用RxJava.

TL;DR It's achievable - use an observer relationship. Your IntentService and likely location service should not be aware of your ViewModel. Consider using a Repository. LiveData can be used (see postValue). It's good for updating the UI (ViewModel to Activity communication) because it's lifecycle-aware. When you're not updating the UI, you could consider RxJava.

这取决于您要遵循的体系结构.如果您执行的操作与 App体系结构指南,您的IntentService可能是由您的远程数据源代码启动的:

It depends on what architecture you're following. If you're doing something similar to what's described in the Guide to App Architecture, your IntentService is probably started by your remote data source code:

您的远程数据源代码将具有一个可观察对象(Rx Flowable,LiveData等),我将其称为可观察对象A,用于您的意向服务下载的数据.您的存储库类(如果使用一个)将具有可观察的b,而ViewModel将具有可观察的c.

Your remote data source code would have an observable (Rx Flowable, LiveData, etc) which I'll call observable A, for the data downloaded by your intent service. You Repository class (if you use one) would have an observable b and your ViewModel would have an observable c.

存储库订阅了您的网络代码中的可观察对象(可观察到的A),ViewModel订阅了存储库中的可观察对象(可观察的B),而您的Activity/Fragment/View订阅了您的ViewModel中的可观察对象(了可观察的c) .然后...

The Repository subscribes to the observable in your networking code (observable A), the ViewModel subscribes to the observable in your Repository (observable B), and your Activity/Fragment/View subscribes to the observable in your ViewModel (observable c). Then...

  1. IntentService取回数据并设置可观察的A
  2. 这会触发您的存储库,因为它已被订阅-它会执行存储库应该执行的数据处理类型,例如将数据保存到数据库中.
  3. 完成存储库后,它将使用新处理的数据设置可观察到的B.
  4. 这会触发您的ViewModel,因为它已被订阅-它执行ViewModels进行数据处理的类型,即格式化数据以便为视图准备就绪,然后设置可观察的C ...
  5. 这会触发您的活动/片段/视图,从而更新用户界面

从根本上说,这一直是一连串的观察者关系.在每个级别上,都将进行适当的处​​理,然后设置一个可观察值,以新数据触发下一个级别.这样可以避免与IntentService/Repository/ViewModel的强烈耦合.

It's basically a long chain of observer relationships all the way up. At each level, the appropriate processing is done, then it sets an observable, which triggers the next level with the new data. This allows you to avoid strong coupling with your IntentService/Repository/ViewModel.

您的服务不会意识到您的ViewModel(如果您有的话,则是Repository),它们应该只设置可观察对象的值.如果要跳过拥有存储库的操作,可以让ViewModel观察远程数据源类,但是如果需要执行任何逻辑(例如将下载的数据保存到数据库中),则可能需要存储库.

Your Services would not be aware of your ViewModel (or Repository if you have one), they should simply set the value of an observable. If you want to skip having a repository, you could have the ViewModel observe your remote data source class, but if you need to do any logic like saving the data you downloaded to a database, you probably want a Repository.

关于LiveData的两个注意事项-如果需要在执行后台操作时更新LiveData,请使用

Two notes about LiveData - If you need to update LiveData when you're doing a background operation, use postValue.

LiveData具有生命周期感知,这使其特别适合用于按生命周期(活动/片段)进行观察.

LiveData is lifecycle-aware, which makes it particularly well suited for observation by things with lifecycles (Activities/Fragments). The observe method takes a LifecycleOwner.

对于存储库/网络代码中的B和A之类的观察者,很可能不会有LifecycleOwner.这意味着您可以执行 observerForever ,或使用其他可观察对象,例如RxFlowable.

For observers like B and A in your Repository/Networking code, there likely won't be a LifecycleOwner. This means either doing something like using observerForever, or using another observable, like an RxFlowable.

这篇关于Android体系结构组件ViewModel-与Service/IntentService的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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