问题标签 [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.
kotlin - 在流程中实施退避策略
我正在尝试仅使用 kotlin 实施退避策略flow
。
我需要从 timeA 到 timeB 获取数据
result = dataBetween(timeA - timeB)
如果结果为空,那么我想使用指数退避来增加结束时间窗口
result = dataBetween(timeA - timeB + exponentialBackOffInDays)
我正在关注这篇文章,该文章解释了如何在rxjava2
.
但是卡在还flow
没有takeUntil
操作员的地方。
你可以在下面看到我的实现。
它工作正常,除非流具有成功的值,流继续收集直到8
从上游流,但理想情况下,它应该在到达5
自身时停止。
关于我应该如何解决这个问题的任何帮助都会有所帮助。
kotlin - 流动> 而不是使用 Flow?
我注意到很多人和示例使用流作为 List<> 的包装器,例如:
据我所知,Flow 就像一种“异步序列”,所以我真的没有理解“Flow<List< T >>”的含义。
为什么我们不能直接使用 Flow< T > 这对我来说似乎很直观,我问这个问题是因为我在代码片段中看到了多次重复并且无法理解其背后的目的?
kotlin - Kotlin Flows: StateFlow or distinct+conflated?
I've got a bizarre file that is always the same size, with the same modified date. (some driver showing its state as a file's contents through a fake filesystem). It contains a short string that changes when the device's state changes. I don't think I can Watch the file because it is a fake filesystem. Cool so far!
I was hoping to modernize it! A changing file that may or may not have anyone listening feels like a flow.
I'm not entirely sure about distinctUntilChanged and conflate (do they play nice together?) and if I'm just reinventing a StateFlow?
This seems to work (with the expected system overhead) and I'm only trimming the deduped values, but... it smells funny. Like I should be able to reuse file handles, or memory mapped stuff, or a mark()/reset(), or better still, wrap it all up in a StateFlow object.
android - Kotlin Flow“首次离线”方法
我正在开发一个全新的 android 应用程序,我想使用 Kotlin Flow 实现离线优先策略,就像我们使用 RxJava 一样。当我使用 Rx 时,我将以下代码用于离线优先功能。
getOffline 和 getRemote 函数将返回一个可观察对象。我使用下面的代码来使用 Flow 实现相同的结果。
getLocal 和 getRemote 将返回一个 Flow 对象。我还在我的一个游乐场项目中使用了另一种逻辑,如下所示:
在“else”部分,它将先发出本地结果,然后发出远程,这就是我在这种情况下首次离线处理的方式。
但我不确定我是否使用了最好的方法。
有人可以建议我一些更好的方法吗?
android - PagingDataAdapter.submitData 不添加数据
我正在使用 jetpack 库中的 Paging 3,在将数据插入房间 db 后,collectLatest 被调用,但在使用 PagingDataAdapter.submitData 添加数据后,它似乎没有添加到适配器中。我尝试使用adapter.refresh 和adpter.notifyDatSetChanged,它也不起作用,下面是完整的代码。
kotlin - 何时为调用 flow.single() 引发 NoSuchElementException
假设我有一个这样的 API:
我像这样消费它:
根据文档,如果流为空,则可以抛出single
a 。NoSuchElementException
但是,这让我很困惑,因为对流的终端操作将“等待”要发出的流元素。那么调用如何single
知道流中没有元素呢?也许一个元素还没有发出?
我的意思是在引擎盖下,调用single
是在进行检查之前收集源流。因此,在执行 null 检查之前必须至少发出 1 个项目,因此 null 检查永远不应该成功并且NoSuchElementException
永远不应该抛出 a(对于流是不可为 null 类型的情况)。
那么NoSuchElementException
只有可空类型的流才有可能吗?
这是源代码single
:
kotlin - 等待循环中的所有流完成
我有一个将数据作为流返回的 API:
当我获取数据时,我需要加载一些项目的详细信息并将结果转换为实时数据:
这里的问题在完成emit(items)
之前调用loadDetails
,所以item.details = details
更新调用。
如何等待forEach
更新所有项目?
android - 在 Kotlin Flow android 中从 OKHTTP 拦截器捕获自定义异常
我正在尝试捕获自定义异常,在我的情况下它是 UserUnAuthorizedException,我正在尝试从 OkHttp 拦截器中抛出并在 Kotlin Flow 中捕获,并且在 catch 块中我能够捕获此异常
java.io.IOException:由于 UserUnAuthorizedException 而取消
但是应用程序崩溃了,我无法理解为什么会这样,下面是我的代码
OkHttp 拦截器
android - 是否可以将 Flow 与 Room 删除或插入操作一起使用,因为它在 RxJava 中使用 Single 或 Maybe 返回行 ID?
打电话
返回编译错误
不能使用带有 Int 或 long 的 Flow 来获取 RxJava 中数据库操作结果的 id 吗?
android - Kotlin Flow 单元测试异常和具有不同调度程序的 FlowOn 作业
使用下面的代码来测试异常Flow
和MockK
@Test 乐趣given network error returned from repos, should throw exception
() = testCoroutineScope.runBlockingTest {
和版画
并且测试失败了
expected not to be: null
我测试的方法是
这不是一个完全实用的功能,只是为了检查 Dispatchers 如何使用 Flow 和测试。
在撰写我评论的问题时.flowOn(Dispatchers.IO)
,测试通过的最重要的问题。还将 Dispatchers.IO 更改为 Dispatchers.Default 并导致测试通过。我认为这是由于使用了不同的线程。
1-是否有一种功能或方法可以在不修改代码的情况下将所有 flowOn 方法设置为同一个线程?
这次我尝试测试成功场景
当我尝试截取被注释掉的片段时,测试失败
job
如果已取消,似乎测试可以通过launchIn
,返回的测试job
通过。
2- 为什么收集作业没有被取消,它只在第一次flowOn
使用时发生Dispatchers.IO
?