问题标签 [android-livedata]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
android - 如何观察 PagedList 数据?
我正在使用分页库和 Android 架构组件。我只是想观察 pagedlist livedata 并在发生更改时更新我的 RecyclerView。
我正在观察 isLoadingLiveData、isEmptyLiveData 和 errorLiveData 对象,它们是在我的 ViewModel 中创建并在我的片段中观察到的 MediatorLiveData 对象。并且还观察从远程返回获取的 Gist 列表的 resultLiveData。
在我的 ViewModel 中,我创建了一个 PagedList LiveData,每当它的数据发生变化时,我都想更新 isLoadingLiveData、isEmptyLiveData、errorLiveData 和PagedListAdapter。因此,我将 isLoadingLiveData、isEmptyLiveData、errorLiveData 和 resultLiveData 定义为 MediatorLiveData 对象。我添加了 resultLiveData 作为这些对象的来源。所以当 resultLiveData 发生变化时,这些对象的 onChanged 方法就会被调用。并且 resultLiveData 依赖于 userNameLiveData,所以当 userNameLiveData 发生变化时,会调用 allGistsLiveData 并获取数据。例如,当用户滑动列表时,我正在设置 userNameLiveData 并再次进行网络调用。
我的视图模型:
和我的片段:
我的存储库:
我的远程数据源:
我试图从这个项目中创建这个解决方案。但是我的问题是 resultLiveData 一直在更改而没有等待网络调用响应,因此响应的结果被忽略了,我的 ui 在数据到达之前就被更新了。由于 resultLiveData 在请求之前正在更改,因此还没有数据。简单地说,我怎样才能观察 pagedlist livedata?
android - 如何在android中的rxjava2中进行错误处理
嗨,我在我的 android 应用程序中RxJava2
使用。LiveData
我正在使用下面的 http 调用
我如何在上面的代码中处理如下错误。
android - Room- LiveData RxJava 无故触发
我有一个 Activity、ViewModel、Singleton 存储库和 Room。在活动中,OnCreate
我创建了 ViewModel:
主视图模型:
存储库具有此功能:
我的道:
我有一个非常奇怪的行为:第一次创建活动时,数据被很好地获取并且一切正常。之后,我在 MainActivity 保持暂停时启动第二个活动。返回主活动后,LiveData 再次被触发,没有明显的原因。
我试图切换LiveDataReactiveStream
到 justdatabase.myDao().observeData()
并LiveData
在 Dao 中返回而不是Flowable
它修复了当活动恢复时该额外触发器的奇怪行为。
但是我需要使用或 rxjava 来进行更复杂的操作和线程。知道为什么会发生触发吗?
android - 扩展 LiveData 的模拟类
我想对依赖于 LocationLiveData 类的存储库进行单元测试:
在我调用 setValue(someLocationDataInstance) 之后,如何使模拟像 liveData 一样发出 LocationData 对象?
更新 1:我想测试以下存储库:
}
android - Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change
I found that the LiveData returned by Dao will call its observer whenever the row is updated in DB, even if the LiveData value is obviously not changed.
Consider a situation like the following example :
Example entity
Example Dao
Somewhere in background thread
Somewhere in UI
In this example, the ui is only interested to user name so the query for LiveData only includes the name field. However the observer.onChanged will still be called on Dao Update even only other fields are updated. (In fact, if I do not make any change to User entity and call UserDao.updateUser, the observer.onChanged will still be called)
Is this the designed behaviour of Dao LiveData in Room? Is there any chance I can work around this, so that the observer will only be called when the selected field is updated?
Edit : I changed to use the following query to update the lastActiveDateTime value as KuLdip PaTel in comment suggest. The observer of LiveData of user name is still called.
android - 如何使架构的 PagedListAdapter 在配置更改中幸存下来?
Android 的新 PagedListAdapter 是一个很棒的库,用于处理数据列表的分页。它对我来说效果很好,只是你如何让它像 Android 架构的 ViewModel 一样在配置更改(例如屏幕旋转)中幸存下来?
android - LiveData 质疑
对实时数据有很多疑问
我在一项活动中调用新的 asynctask 服务。响应是用户订单列表。我使用 JSON 解析在响应中显示 ui 中的名称和值。如果我立即更改了 mysql 数据库中的值,它不会在 ui 中更改。当我来自以前的活动时,它会更改,然后值更改它是完美的。但是我希望它一旦对数据库产生影响就立即更改是可能的
但是实时数据意味着它会立即对 ui 产生影响,但不会改变
android - 使用实时数据时如何在 Android 中链接转换?
鉴于以下设置:
我有 2 个存储库:存储库 A和存储库 B,它们都返回实时数据。
我有一个使用这两个存储库的 ViewModel。
我想从存储库 A 中提取一些东西,根据结果我想从存储库 B 中获取一些东西,然后在返回 UI 之前转换结果。
为此,我一直在研究LiveData Transformation类。这些示例显示了结果的单个转换,但是我想要一些类似于链接两个转换的东西。我怎样才能做到这一点?
我尝试过这样设置,但在第二个转换块上出现类型不匹配:
(另外请让我知道是否有替代/推荐的方法来获取链接这些调用的东西,即从 A 获取一些东西,然后根据 A 的结果从 B 获取一些东西等等)
android - Force LiveData Observer inside a Fragment to receive a change after coming visible again?
I am implementing Android Architecture Components
. Imagine a case like the following where your Fragment
is observing a LiveData
to change its UI. User minimizes the app and the state is changed (in my case from the repository). So the Observer
from the Fragment
is not triggered with a change because the Fragment
is not visible. But then, when the user comes back to the app it doesn't trigger the new state. If the state is changed again (while the Fragment
is visible), the Observer
receives the change. Do you know any way to force an update when fragment is visible again?
android - 如何更新 LiveData 值?
我正在为我的数据使用新的分页库。创建 ViewModel 并首次初始化实时数据时,一切正常。问题是当我点击菜单项并想用一组不同的数据更新它时,我无法更新我的实时数据的值。然后我的片段中的 onChanged 方法不会被调用。我已阅读有关 MutableLiveData 以及 setValue 和 postValue 等可以更新实时数据的方法,但在我的情况下,我使用的是 LivePagedListProvider 并且无法从数据库返回 MutableLiveData。
道:
分段:
视图模型: