相关疑难解决方法(0)

LiveData与Handler和LocalBroadcast

我有旧的Android/Java代码,它包含两个派生自IntentService,而这些服务不在单独的进程中运行.

问题是关于从这些结果返回结果的方法IntentService.

一个服务返回结果使用Handler+ Runnable,在主循环中运行代码:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        MyApplication.get().setFoo(someThing);
    }
});
Run Code Online (Sandbox Code Playgroud)

另一个LocalBroadcastManager.getInstance(this).sendBroadcast(in);用于发送消息Activity,并Activity通过BroadcastReceiver消息onResume订阅,并取消订阅onPause.

我是对的,在这两种情况下都可以LiveData用来简化事情吗?

IntentService应该创建LiveData和谁想要结果observe,当新数据到达IntentService应该打电话postValue,或者可能有一些珊瑚礁,以防止在LiveData这里使用?

java android

18
推荐指数
1
解决办法
942
查看次数

将LiveData转换为MutableLiveData

显然,Room无法处理MutableLiveData,我们必须坚持使用LiveData,因为它返回以下错误:

error: Not sure how to convert a Cursor to this method's return type
Run Code Online (Sandbox Code Playgroud)

我用这种方式在我的数据库助手中创建了一个"自定义"MutableLiveData:

class ProfileRepository @Inject internal constructor(private val profileDao: ProfileDao): ProfileRepo{

    override fun insertProfile(profile: Profile){
        profileDao.insertProfile(profile)
    }

    val mutableLiveData by lazy { MutableProfileLiveData() }
    override fun loadMutableProfileLiveData(): MutableLiveData<Profile> = mutableLiveData

    inner class MutableProfileLiveData: MutableLiveData<Profile>(){

        override fun postValue(value: Profile?) {
            value?.let { insertProfile(it) }
            super.postValue(value)
        }

        override fun setValue(value: Profile?) {
            value?.let { insertProfile(it) }
            super.setValue(value)
        }

        override fun getValue(): Profile? {
            return profileDao.loadProfileLiveData().getValue()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,我从DB获取更新并可以保存 …

android kotlin android-room android-livedata android-architecture-components

8
推荐指数
3
解决办法
6925
查看次数

在前台服务中实现 Room 时的 ViewModel

我目前有一个应用程序,其中包含用于所有服务器/API 交互的 ForegroundService 和用于本地持久性的 Room 数据库。我一直在尝试实现 AndroidViewModel 来帮助数据持久化和快速 UI 刷新。

然而,根据文档,ViewModels 不能在 Services 中实现,到目前为止,我已经使用 Service 在本地更新信息并使用 LocalBroadcasts 通知组件(这是我想使用 ViewModels 和 Observers 消除的)。

我需要让服务运行,因为应用程序需要在后台继续运行(它是一个关键任务应用程序,应用程序关闭意味着用户将无法提供关键服务),并定期更新某些信息(附近的请求等)。

所以要问核心问题——

  1. 如何将服务与 ViewModel 分开,如果服务具有来自服务器的最新同步数据,如何更新 ViewModel 中的(可变)LiveData 列表?
  2. 这篇文章对 SO 问题的回答 说最好将 ViewModel 与存储库分开,而另一篇文章则给出了在 ViewModel 中包含 Room 数据库的示例。哪个是更好的选择?

我的一些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; …
Run Code Online (Sandbox Code Playgroud)

android mvvm android-service android-viewmodel

4
推荐指数
1
解决办法
2970
查看次数