问题标签 [kotlin-flow]
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 - Flow LifeCycle 是否可以识别为 LiveData?
我们知道LiveData 是生命周期感知的,如果配置更改 LiveData 对象不会每次都从数据库(本地/远程)重新查询,并且只有在数据中有任何更新时才会更新。
最近我开始使用Kotlin Flow,我应该承认它最适合数据层,即在 repo 中实现,以便通知 ViewModel。但是我在 ViewModel/View 层中也使用了Kotlin Flow,以便我collect
直接根据其状态(密封类实现)在 Fragment 中的 Flow 对象。我在使用 Flow 时遇到的问题是每次从数据库(本地/远程)检索配置更改数据时。
在这种情况下应该怎么做?有没有办法避免在使用 Flow 时重新查询,或者我应该只在 ViewModel/View Layer 中使用 LiveData?
示例代码
回购:
视图模型:
分段:
kotlin - Kotlin 协程流程未取消
我有一个 UseCase 类,其方法返回 Flow
我正在像这样在 ViewModel 中收集发布日期
我的片段正在观察 LiveData 并显示值。在我离开屏幕之前,它会按预期工作。正如launch
用 ViewModel 的 coroutineScope 调用的那样,它不应该被取消并且不再发出值吗?
ViewModel 的范围在 BaseViewModel 中实现,我的 ViewModel 扩展如下:
抽象类 BaseViewModel(private val dispatcherProvider: DispatcherProvider) : ViewModel(), CoroutineScope {
}
我是否忘记添加一些自定义取消逻辑或缺少其他内容?
kotlin - Kotlin Flow,使流程有条件 - 仅在 true 时进行调用
刚开始使用 Kotlin 流程。如果记录是唯一的,我基本上只想将一个项目插入房间数据库。我想我可能可以做到这一点,@Insert(onConflict = OnConflictStrategy.REPLACE)
但imageUrl
永远是独一无二的。我总是需要检查plateText。
到目前为止,我已经得到了这个,我正在寻找可以优化它的方法。
欢迎任何想法。
android - Kotlin Flow.collect executes but does not update ui onConfigurationChanged
I'm using Flow
to get data from Room then I call Flow.collect
in my FragmentLogic class. Inside collect{}
I do some work and update the View through an Interface( example: view.updateAdapter(collectData)
). This works fine until an onConfigurationChanged
is called and the screen rotates, the code inside collect executes and works in Logcat but anything that changes the UI does not work, the function updateAdapter()
is called however nothing happens. One solution I found is to call the function beginObservingProductList()
again in onStart()
if savedinstance is not null but that creates two instances of the collect{} and they both show in logcat.
I want the UI changes to work even after onConfigurationChanged
is called.
Room Dao class:
then in the implementation:
And finally I collect the data and change the view:
unit-testing - 当使用返回流的存储库对视图模型进行单元测试时,将其转换为实时数据时会发生错误
我需要一些关于在 android 中编写单元测试的帮助,这些测试与 viewmodel、livedata 和流机制以及调度有关。
首先,我正在编写单元测试,而不是仪器测试。
实际上,我为 Android 应用程序创建了一个单元测试,用于测试使用存储库从互联网获取一些数据的 ViewModel。
我使用的视图模型的代码是这样的:
单元测试代码如下:
创建单元测试并运行它时,会发生以下错误:
至于错误,似乎我需要将参数传递给viewmodel.data值,但是,哪个?根据代码,它不需要参数。
我想了解模拟返回流对象的方法,因为asLiveData()函数是在运行测试时抛出上述异常的函数。
另外,我认为我需要了解用于执行和观察来自 livedata 的值的observeForever函数,毕竟,然后观察我可以在哪里断言单元测试的结果。
任何帮助都会很棒。:)
我在应用程序 build.gradle 文件中使用以下库:
android - DataStore 未收到来自 Flow 的事件
我正在使用 datastore-preferences:1.0.0-alpha01 并且在更新数据存储区值时似乎无法恢复事件。我一直试图在片段和父活动中以相同的结果观察它。当我创建 DataStore 的实例并观察值流时,我在声明观察者后立即收到一个事件(在注册观察者后似乎很常见)。这将表明流程正在运行,并且观察者正在按预期接收事件。然后,我在从不接收事件的同一观察者内部更新首选项的值。
我使用这篇文章作为参考https://medium.com/scalereal/hello-datastore-bye-sharedpreferences-android-f46c610b81d5与我用来比较我的实现的存储库一起使用。显然,这个有效。https://github.com/PatilShreyas/DataStoreExample
数据存储实用程序
更新连接对话框
当视图膨胀时,这就是我在日志中看到的
现在,通过其他示例,我们应该看到观察者触发了一个已更新的事件,但我没有。对于其他示例,添加相同的日志记录点,很明显观察者收到了更新事件。
android - Kotlin:CoroutineScope 即使在流程取消后仍在收集数据
我正在尝试Coroutine
在我的内部启动一个PagingSource
,以查看我的分页源已经尝试获取我的数据多长时间。我在这里遇到的唯一问题是Coroutine
,即使我停止了shopPagingWatcher Flow
. 正因为如此,它throws IOException("No Intenet Exception)
即使不应该。
我正在启动一个Coroutine
因为看状态不应该阻塞我的主流paging source
寻呼源
ShopPagingWatcher
抽象工作观察者
我应该如何改变我的Coroutine
内心PagingSource
以实现我的目标?Timber.d("Mediator stopped)
被调用。
我感谢每一个帮助,谢谢。
kotlin - 链式 kotlin 流取决于结果状态
我正在寻找实现以下逻辑的最“干净”的方式:
- 我有N个方法,每个人都返回Flow<Result<SOME_TYPE>>(类型不同)
- 我想链接这些方法,所以如果 1 返回 Result.Success,然后调用 2nd 等等。
最明显的方法是:
但它看起来像一个众所周知的“回调地狱”。你有任何想法如何避免它?
android - Kotlin Flow - 可空值
是否可以检查 Flow 是否发回一个值,如果没有则对其采取行动?
android - Fragment与ViewLifecycleOwner的偏差
我正在收集流量viewLifecycleOwner
。它继续流动Dispatchers.Default
,但收集本身发生在Dispatchers.Main
.
在一个场合IllegalStateException
,我发现该片段未附加。
IllegalStateException:片段测试未附加到上下文。
我假设在分离片段之前会取消流的收集。
协程如何在分离的片段上恢复?