问题标签 [kotlinx.coroutines]

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.

0 投票
1 回答
946 浏览

kotlin - 无法具体化传递给 forEach 的可暂停块

鉴于一些

这有效:

但这在编译时失败:

此外,它无法修复,因为:

就静态类型系统而言,这里的故事是什么?当前情况看起来像是一个快速破解,其中包含suspend fun调用的内联块仍被推断为非挂起类型签名。

这是一个在最终确定之前需要改进设计的领域吗?

0 投票
4 回答
2662 浏览

android - Kotlin 协程:在测试 Android Presenter 时切换上下文

我最近开始在我的 Android 项目中使用 kotlin 协程,但我遇到了一些问题。许多人会称之为代码异味。

我正在使用 MVP 架构,其中协程在我的演示者中启动,如下所示:

launchAsync功能以这种方式实现(在我的 WorklistPresenter 类扩展的 BasePresenter 类中):

问题在于我使用的是依赖于 Android 框架的 UI 协程上下文。我无法将其更改为另一个协程上下文而不遇到ViewRootImpl$CalledFromWrongThreadException. 为了能够对此进行单元测试,我创建了 BasePresenter 的副本,其中包含以下不同的实现launchAsync

对我来说这是一个问题,因为现在我的 BasePresenter 必须在两个地方进行维护。所以我的问题是。如何更改我的实现以支持简单的测试?

0 投票
2 回答
11804 浏览

android - kotlin 协程抛出 java.lang.IllegalStateException: 已经恢复,但得到值 Location

总的来说,我对 Kotlin 协程和 Android 开发很陌生。在玩弄以了解它是如何工作的时,我遇到了一个似乎无法解决的错误。

从一项基本活动中,我尝试连接到 googleApiClient。权限没问题。我希望使用 kotlin 协程以直接方式从 LocationManager 获取位置更新,以便稍后使用此 Location 对象。我第一次在模拟器中更改位置时它工作正常,第二次更改位置时,它崩溃,异常如​​下:

就好像 Kotlin 使用相同的延续。我不知道我做错了什么以及为什么它第二次崩溃。有人可以启发我。提前Thks。

0 投票
1 回答
2747 浏览

intellij-idea - 如何使用 Kotlin DSL 为 Gradle 配置 Kotlin 协程?

使用带有 Kotlin-DSL 的 Gradle 4.4.1,我想在我的 Kotlin 1.2.20 项目中配置实验性的 Kotlin 协程。这被证明是非常困难的。

Kotlin 官方文档本身只提到了带有 Groovy DSL 的 Gradle,而不是 Kotlin。

Kotlin-DSL 项目有一个不起作用的协程示例。可能是针对较旧的 Gradle 版本(该脚本中的 Kotlin 版本是 1.1.51,这本身就很可疑)。

PS:即使是 IntelliJ 的configure Kotlin也会编写不正确的 Gradle 代码。降级是唯一的选择吗?


附录:在 IntelliJ IDEA 中构建脚本非常红。IDE 一直在添加导入语句。似乎这些徒劳的尝试使脚本无法正常工作。我暂时禁用了“即时添加明确的导入”,即使脚本大部分保持红色,Gradle 也可以执行它。之后,IntelliJ 的错误通知也消失了。

0 投票
1 回答
1733 浏览

android - 使用协程调用适配器工厂取消请求的改造 2

我正在尝试使用带有协程调用适配器工厂的retrofit2在我的android应用程序中实现动态搜索功能。当用户键入关键字并且关键字长度有效时,应用程序向服务器发出请求。在单个请求中,我可以请求如下

但是如果我想在用户更改可编辑项时取消每个先前的请求并发出新请求怎么办?我搜索了很多例子,但我找不到任何有用的东西。感谢帮助。

0 投票
3 回答
2505 浏览

kotlin - 如何使用 kotlinx-coroutines-reactive 处理空场景?

kotlinx-coroutines-reactive使org.reactivestreams.PublisherawaitXXX方法:

如果通过人员 ID 找不到人员,则此调用将抛出 NoSuchElementException 并且无法在用户代码中直接处理此异常。而 Spring MVCExceptionHandler无法将此异常转换为用户友好的响应。

我能想到的一种方法如下:

但我不认为这是一种优雅的方式。例如,可以提供一个名为awaitSingleOptional.

有没有更好的 Kotlin 方法来处理这种情况?

0 投票
1 回答
607 浏览

android - 使用 RxJava 或 kotlin 协程的 Android ViewState

我正在尝试学习如何在 Android 中使用 RxJava,但遇到了死胡同。我有以下数据源:

每当我更改 的值时,我都想触发下游更新relay,但这不会发生。它在Activity初始化时起作用,但在我更新值时不起作用。这是我的 ViewModel,我从中更新值:

这是执行订阅的代码Activity

0 投票
0 回答
222 浏览

unit-testing - 将 kotlin 扩展功能与接收器一起使用时的单元测试

是否可以在单元测试中将特定值传递给带有接收器的扩展函数?

我正在尝试测试以下订阅方法

我希望能够将特定值应用于consumeEach函数,但如何做到这一点?

这是我的单元测试:

0 投票
0 回答
153 浏览

android - Anko Coroutines - 异步 BG 不返回任何内容

我是 Anko 的新手,我想在后台线程上执行一些工作,所以我找到了 couroutines 解决方案(使用 kotlin)

于是我使用了Anko couroutines -> 并找到了这个bg方法在后台取得了进展。

但是后处理执行不起作用。任何想法?

我尝试了 await() 和 completed() ,但没有任何效果。

0 投票
0 回答
1332 浏览

android - Kotlin Coroutine to escape callback hell

I'm trying to use Kotlin's coroutines to avoid callback hell, but it doesnt look like I can in this specific situation, I would like some thougths about it.

I have this SyncService class which calls series of different methods to send data to the server like the following: SyncService calls Sync Student, which calls Student Repository, which calls DataSource that makes a server request sending the data through Apollo's Graphql Client. The same pattern follows in each of my features:

SyncService -> Sync Feature -> Feature Repository -> DataSource

So every one of the method that I call has this signature:

The problem is: When I sync and successfully save the Student on server, I need to sync his enrollment, and if I successfully save the enrollment, I need to sync another object and so on. It all depends on each other and I need to do it sequentially, that's why I was using callbacks. But as you can imagine, the code result is not very friendly, and me and my team starting searching for alternatives to keep it better. And we ended up with this extension function:

But the function in DataSource still has a onSuccess() and onError() as callbacks that needs to be passed to whoever call it.

The SyncService class code changed to be like:

It does execute it sequentially, but I need a way to stop every other coroutine if any got any errors. Error that might come only from Apollo's So I've been trying a lot to find a way to simplify this code, but didn't get any good result. I don't even know if this chaining of callbacks can be simplify at all. That's why I came here to see some thoughts on it. TLDR: I want a way to execute all of my functions sequentially, and still be able to stop all coroutines if any got an exception without a lot o chaining callbacks.