https://kotlinlang.org logo
Docs
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
coroutines
  • p

    pakoito

    12/12/2017, 12:34 AM
    do you please have a minimal example?
    a
    • 2
    • 1
  • l

    louiscad

    12/12/2017, 9:19 AM
    Hi, is there a way to throw an exception on the next channel suspended
    receive()
    without closing the channel, thus allowing subsequent attempts to get a value from the channel? I assume
    Channel
    is not the right choice, but after reading the whole kotlinx.coroutines README, I did not find what I'm looking for.
    e
    • 2
    • 9
  • l

    louiscad

    12/12/2017, 10:55 AM
    Another question. Why
    Mutex
    is not reentrant? I'd love to have it for transactional stuff ("reliable writes" in BluetoothGatt). Is there an alternative? FYI, here's how I want to use it: https://gist.github.com/LouisCAD/88d06da6ae1caf04acbf6cd4fc1d5cac#file-gattconnection-kt-L56 EDIT: Added line of the relevant method in the link
    e
    • 2
    • 4
  • l

    littlelightcz

    12/12/2017, 6:31 PM
    Asad: I just wanted to try to convert the callback API of OkHttp into "synchronous calling style" of coroutines
    a
    • 2
    • 1
  • l

    littlelightcz

    12/12/2017, 6:41 PM
    @asad.awadia yeah but they're using CountDownLatch - this is what I expected coroutines will let me avoid 🙂
    g
    • 2
    • 1
  • p

    pakoito

    12/13/2017, 1:12 AM
    there’s blocking wait and blocking await
    g
    • 2
    • 1
  • d

    diesieben07

    12/13/2017, 1:20 AM
    But if you need to access blocking I/O, do not do it in a coroutine.
    a
    g
    +3
    • 6
    • 8
  • d

    diesieben07

    12/13/2017, 1:22 AM
    The coroutine does not do the blocking. You have to do the blocking in a separate thread.
    a
    • 2
    • 1
  • d

    diesieben07

    12/13/2017, 1:22 AM
    That awaiting is non blocking.
    a
    g
    • 3
    • 2
  • d

    diesieben07

    12/13/2017, 1:23 AM
    So with your count down latch example, the
    latch.countDown()
    etc. can not happen in the coroutine, it must be in a different thread.
    a
    • 2
    • 1
  • d

    dave08

    12/13/2017, 8:30 AM
    @louiscad You can't have an actor (maybe with a buffer or conflated) receiving all requests, and handling what to do if there's no connection?
    l
    • 2
    • 2
  • e

    elizarov

    12/13/2017, 8:41 AM
    @louiscad I'd define
    val connectionStatus = ConflatedBroadcastChannel<Boolean>(false)
    to keep connected/disconnected status and then whenever I need to wait for it being connected I'd do
    connectionStatus.consume { while (!receive()) { /* do nothing */ } }
    l
    • 2
    • 4
  • l

    louiscad

    12/13/2017, 8:56 AM
    @qwert_ukg
    async
    can also work if you (dangerously) want to also forget about potential exceptions
    q
    • 2
    • 2
  • g

    gildor

    12/13/2017, 8:57 AM
    imo
    async
    shouldn’t be used if you don’t need result. Probably make sense to write some style guide for kotlinx.coroutines (especially for API). We have so many useful idioms and best practices even now
    👍 6
    q
    • 2
    • 1
  • e

    elizarov

    12/13/2017, 10:19 AM
    A friendly reminder: https://stackoverflow.com/questions/46226518/what-is-the-difference-between-launch-join-and-async-await-in-kotlin-coroutines
    e
    • 2
    • 4
  • d

    dekans

    12/14/2017, 10:46 AM
    coroutines are a good way, Elizarov posted an example https://stackoverflow.com/a/46890009/3485324
    n
    • 2
    • 2
  • l

    louiscad

    12/14/2017, 1:28 PM
    What's the difference between
    send()
    and
    offer()
    when using
    RendezVousChannel
    apart from the fact that
    offer()
    is not suspended?
    e
    d
    • 3
    • 5
  • g

    galex

    12/14/2017, 8:17 PM
    Hi, after upgrading to Kotlin 1.2.0 on Android, a gradle plugin that uses coroutines (also compiled with Kotlin version 1.2.0) and since this upgrade when I run a task from Gradle to run this plugin I get the following error: Caused by: java.lang.NoSuchMethodError: kotlinx.coroutines.experimental.DeferredKt.async$default(Lkotlin/coroutines/experimental/CoroutineContext;Lkotlinx/coroutines/experimental/CoroutineStart;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lkotlinx/coroutines/experimental/Deferred; Any ideas why? Thank you in advance!
    l
    e
    • 3
    • 2
  • b

    billybong

    12/15/2017, 9:28 AM
    Can a coroutine actor send to it’s own channel?
    e
    m
    • 3
    • 10
  • e

    elizarov

    12/15/2017, 10:59 AM
    You can use
    CompletableDeferred<Unit>()
    as a latch. On one side you
    await()
    it, on the other you do
    complete(Unit)
    .
    f
    • 2
    • 3
  • r

    r4zzz4k

    12/15/2017, 1:39 PM
    Do I understand correctly that there are no ways to have observable collection out of the box? So if I want observable list, I have to have either a channel holding either new copy of the list or incremental changes depending on the use-case?
    e
    • 2
    • 5
  • z

    zak.taccardi

    12/15/2017, 4:06 PM
    my company needs a future type (on Java 7, so no
    CompletableFuture<T>
    . But we have mixed kotlin/java code. Does that mean we cannot use
    Deffered<T>
    ?
    r
    • 2
    • 1
  • p

    pakoito

    12/15/2017, 8:20 PM
    I don’t need the bells & whistles of cancellation and such
    p
    • 2
    • 2
  • n

    nil2l

    12/16/2017, 1:55 PM
    Are you using coroutines in production? No problems (e.g. on old devices)? (Android)
    v
    k
    l
    • 4
    • 5
  • a

    andyb

    12/18/2017, 1:42 PM
    Hi, just starting to ge my head around coroutines. Love them but have a question. I am using Channels as a queue between coroutines and would like to receive a message but timeout if no message appears within say 1 second. I am currently using
    withTimeout(1000){ incoming.receive() }
    to do this but it seems to be hanging rather than timing out. Am I missing something?
    r
    e
    • 3
    • 4
  • k

    karelpeeters

    12/18/2017, 4:18 PM
    Hey, I'm just starting with coroutines as well. Is there a way to stop a coroutine from inside? Something like this:
    fun main(args: Array<String>) = runBlocking {
        myFun { if (it == 10) return@myFun }
    }
    
    suspend fun myFun(call: suspend (Int) -> Unit) {
        for (i in 0 until 50) {
            call(i)
            println(i)
        }
    }
    , but the coroutine just keeps running. I also tried
    coroutineContext.cancel()
    on line 2, but that doesn't work either and throws an exception.
    d
    • 2
    • 4
  • k

    karelpeeters

    12/19/2017, 4:29 PM
    Maybe
    fun suspend(block: suspend () -> Unit) = block
    works?
    r
    e
    • 3
    • 5
  • d

    Dan T

    12/19/2017, 6:03 PM
    i'm stuck on an issue where i'm going from an akka stream via function to a suspended function, but the suspended function isn't returning. I presume I'm doing something wrong, but don't know how to narrow the issue further. any tips? p.s. are there tracing facilities for coroutines?
    private fun processMessage(record: KafkaMessage) {
        runBlocking {
            launch {
                handler.doSomething(record.data) // its implementation simply returns
                log.debug("2") // NOT CALLED
            }.join()
        }
    }
    d
    e
    • 3
    • 4
  • w

    wineluis

    12/19/2017, 9:46 PM
    can I use blocking calls (i.e. to sql db) on a "Cooperative single-thread multitasking" environment? see https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#cooperative-single-thread-multitasking
    g
    • 2
    • 1
  • l

    louiscad

    12/19/2017, 9:51 PM
    Hi, is there a way to prohibit suspension in inline lambdas? In non inline, you have to explicitly allow it with the
    suspend
    keyword, but in inline, it's implicitly always allowed, by design it seems
    d
    e
    • 3
    • 2
Powered by Linen
Title
l

louiscad

12/19/2017, 9:51 PM
Hi, is there a way to prohibit suspension in inline lambdas? In non inline, you have to explicitly allow it with the
suspend
keyword, but in inline, it's implicitly always allowed, by design it seems
d

dave08

12/20/2017, 3:26 AM
It's not really allowing or not - all the suspend keyword does (simply speaking) is add a callback param to your function that gets called when the continuation is resumed after suspention... inline doesn't need that since the code inside it is expanded in the calling site by the compiler, and of the calling site is suspendavle or in a coroutine, then it is 'allowed', if not, not. If that's what you meant?
e

elizarov

12/20/2017, 6:58 AM
Not yet. See https://youtrack.jetbrains.com/issue/KT-17260 You are welcome to add your own use-cases there
👍🏽 1
View count: 1