Android 架构组件 ViewModel - 与 Service/IntentService 通信 [英] Android Architecture Components ViewModel - communication with Service/IntentService

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

问题描述

我正在探索 Google 的

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

Repository 订阅你的网络代码中的 observable(observable A),ViewModel 订阅你的 Repository 中的 observable(observable B),你的 Activity/Fragment/View 订阅你的 ViewModel 中的 observable(observable c).然后……

  1. IntentService 取回数据并设置可观察的 A
  2. 这会触发您的存储库,因为它已订阅 - 它执行存储库应该执行的数据处理类型,例如将数据保存到数据库中.
  3. 当您的存储库完成后,它会使用新处理的数据设置 observable B.
  4. 这会触发您的 ViewModel,因为它已订阅 - 它执行 ViewModel 所做的数据处理类型,即格式化数据以便为视图做好准备,然后设置 observable C...
  5. 这会触发更新 UI 的 Activity/Fragment/View

它基本上是一长串一直向上的观察者关系.在每一层,适当的处理完成,然后它设置一个可观察的,它用新数据触发下一层.这使您可以避免与 IntentService/Repository/ViewModel 强耦合.

您的服务不会知道您的 ViewModel(或 Repository,如果您有的话),他们应该简单地设置一个 observable 的值.如果您想跳过存储库,您可以让 ViewModel 观察您的远程数据源类,但如果您需要执行任何逻辑,例如将您下载的数据保存到数据库中,您可能需要一个存储库.

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

LiveData 是生命周期感知,这使得它特别适合用于观察具有生命周期的事物(活动/片段).observe 方法需要一个 LifecycleOwner.

对于存储库/网络代码中的 B 和 A 等观察者,可能不会有 LifecycleOwner.这意味着要么使用 observerForever,或使用其他可观察对象,如 RxFlowable.

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 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.


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:

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.

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 gets data back and sets observable A
  2. This triggers your Repository because it's subscribed - it does the type of data processing the repository is supposed to do, like saving the data to a database.
  3. When your repository is done, it sets observable B with the newly processed data.
  4. This triggers your ViewModel because it's subscribed - it does the type of data processing ViewModels do, namely formatting the data so that it's ready for the view, then sets observable C...
  5. This triggers your Activity/Fragment/View which updates the UI

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.

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.

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

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

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天全站免登陆