kil*_*ahn 4 android mvvm android-service android-viewmodel
我目前有一个应用程序,其中包含用于所有服务器/API 交互的 ForegroundService 和用于本地持久性的 Room 数据库。我一直在尝试实现 AndroidViewModel 来帮助数据持久化和快速 UI 刷新。
然而,根据文档,ViewModels 不能在 Services 中实现,到目前为止,我已经使用 Service 在本地更新信息并使用 LocalBroadcasts 通知组件(这是我想使用 ViewModels 和 Observers 消除的)。
我需要让服务运行,因为应用程序需要在后台继续运行(它是一个关键任务应用程序,应用程序关闭意味着用户将无法提供关键服务),并定期更新某些信息(附近的请求等)。
所以要问核心问题——
我的一些ViewModel代码如下:
public class HouseCallViewModel extends AndroidViewModel {
private String TAG = HouseCallViewModel.class.getSimpleName();
private MutableLiveData<List<HouseCall>> housecallList;
private MutableLiveData<List<HouseCall>> openHousecalls, confirmedHousecalls, closedHousecalls, missedHousecalls, userCancelledHousecalls, respCancelledHousecalls;
private MutableLiveData<List<Incident>> incidentList, openIncidents;
private MutableLiveData<List<Incident>> closedIncidents, usercancelIncidents, respcancelIncidents;
RevivDatabase database;
Context context;
public HouseCallViewModel(Application application) {
super(application);
// DANGER WILL ROBINSON
context = application.getApplicationContext();
database = Room.databaseBuilder(this.getApplication(),
RevivDatabase.class, application.getResources().getString(R.string.database)).build();
}
public LiveData<List<HouseCall>> getHousecallList() {
if (housecallList == null) {
housecallList = new MutableLiveData<>();
loadHousecalls(); // need to call API and sync
}
return housecallList;
}
public LiveData<List<HouseCall>> getIncidentList() {
if (incidentList == null) {
incidentList = new MutableLiveData<>();
loadIncidents(); // need to call API and sync
}
return housecallList;
}
// other constructors, getters and setters here, and functions to update the data
}
Run Code Online (Sandbox Code Playgroud)
小智 5
1)
由于您没有提供有关您的服务及其相关组件的代码详细信息,因此这个答案是抽象的。
要将 ViewModel 与 Service 分离,请创建一个将访问 ViewModel 的 Activity;您将拥有一个 Activity、一个 ViewModel 和一个 Service。
这意味着您将创建一个绑定服务(https://developer.android.com/guide/components/services#CreatingBoundService,更具体地说, https: //developer.android.com/guide/components/bound-services) 。绑定的服务提供了一个接口,活动可以使用该接口与服务进行交互。
绑定服务的一个很好的例子是 Google 的位置更新服务:https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService/app/src/main/java/com/google/android/gms /位置/样本/locationupdatesforegroundservice
在您的实例中,服务的任务是生成数据并将该数据传输到 Activity,然后 Activity 将该数据提供给 ViewModel。
要将数据从服务传输到 ViewModel,我建议使用 Greenrobot 的 EventBus ( http://greenrobot.org/eventbus/documentation/how-to-get-started/ )。
每当您希望 Service 将数据传输到 ViewModel 时,对 Service 中的 EventBus 的单行调用就会将数据传输到 Activity 中正在侦听该类型数据的订阅者。
Activity 在收到数据后,将使用该数据更新 ViewModel。任何注册到 ViewModel 的观察者都将收到最新的数据。
2)
关注点分离原则有利于将 ViewModel 与存储库分离。ViewModel 应该只关心保留将显示给用户的数据的状态,并在配置更改时保留此类状态。