https://kotlinlang.org logo
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
  • d

    Dmytro Danylyk

    03/07/2017, 10:06 AM
    You mean use
    runBlocking
    in test function to wrap my original code which uses
    launch(CommPool)
    ?
    e
    • 2
    • 12
  • b

    bamdmux

    03/07/2017, 12:39 PM
    @elizarov I haven't seen the MainThread or UiThread annotation very often, but I'd say others are pretty common (Nullable, NonNull, IntDef...)
    e
    d
    k
    • 4
    • 6
  • k

    kingsley

    03/07/2017, 1:58 PM
    AndroidDispatcher.Main
    ?
    e
    • 2
    • 4
  • o

    orangy

    03/07/2017, 2:47 PM
    Primary
    ?
    😕 4
    r
    m
    • 3
    • 5
  • j

    jw

    03/07/2017, 6:58 PM
    Actually UI is more correct than main thread since you can technically have different main and UI threads (although it won't happen in end-user applications)
    👍 1
    🤔 3
    b
    • 2
    • 1
  • m

    mfulton26

    03/07/2017, 7:04 PM
    is there any type of
    Iterable<T>.forEachConcurrently
    ?
    e
    • 2
    • 3
  • v

    v0ldem0rt

    03/09/2017, 5:23 AM
    @elizarov I remember you were working on document for UI programming
    e
    • 2
    • 1
  • r

    Ruckus

    03/09/2017, 7:45 PM
    @elizarov In the coroutines GUI guide, is event conflation something that will come out in the next release?
    e
    • 2
    • 1
  • k

    kenkyee

    03/12/2017, 11:15 AM
    How would you do callback bridging with coroutines? It's fairly common to do something like that to convert listeners to Observables in Rx
    e
    • 2
    • 2
  • o

    orangy

    03/13/2017, 5:31 PM
    Since okhttp uses thread pool, it won’t give you much in terms of throughput…
    a
    j
    • 3
    • 3
  • p

    pabl0rg

    03/14/2017, 8:22 AM
    With all of the innovation and standards that have gone into Reactive Streams lately, what is the main benefit of using coroutines instead?
    a
    • 2
    • 2
  • d

    deviant

    03/14/2017, 9:06 AM
    what is the main benefit of using coroutines instead?
    non-blocking code? 🙂
    p
    g
    • 3
    • 4
  • g

    groostav

    03/14/2017, 10:03 PM
    can Breadth First Search be simplified with coroutines? eg if i have a tree of `Node`'s where I want to do a BFS for a node that passes a certain
    Predicate<Node>
    , can I use a
    suspend fun
    to avoid all the queue
    offer
    and
    remove
    malarky?
    e
    • 2
    • 1
  • k

    kirillrakhman

    03/15/2017, 9:55 AM
    @elizarov does kotlinx-couroutines have a mechanism for automatically unsubscribing observables, e.g. when the enclosing activity finishes?
    e
    d
    k
    • 4
    • 7
  • k

    kirillrakhman

    03/15/2017, 11:57 AM
    @elizarov wouldn't it make sense to check if the current thread is the desired thread before calling
    <http://handler.post|handler.post>(block)
    in
    HandlerContext
    ?
    e
    • 2
    • 1
  • k

    kirillrakhman

    03/15/2017, 12:38 PM
    I'm getting
    Warning:Ignoring Android API artifact com.google.android:android:2.3.1 for debug
    . Can the dependency in kotlinx-coroutines-android be declared as compile time only so that the warning disappears?
    e
    • 2
    • 1
  • r

    robin

    03/15/2017, 2:33 PM
    @elizarov Good catch, thanks! Right now I'm only calling it from UI thread, but who knows what else I'll do with it once I have forgotten about that. I've updated the snippet, is it sufficient like that or does the standard synchronization not play nice with coroutines in some way?
    e
    • 2
    • 2
  • g

    gildor

    03/17/2017, 12:58 AM
    Nice talk about what is under the hood of llvm coroutines:

    https://www.youtube.com/watch?v=Ztr8QvMhqmQ▾

    v
    d
    • 3
    • 7
  • g

    gildor

    03/17/2017, 12:59 AM
    I’m really curious will guys from kotlin-native team reuse native llvm implementation in the future or will create new one
    e
    • 2
    • 1
  • e

    elizarov

    03/17/2017, 10:28 AM
    The problem of “waiting forever” in the case of @uhe example of is called “communication deadlock”. It is not directly related to coroutines. It can happen with any channel-based communication abstraction, like distributed apps. It is not a big problem in practice if you follow established patterns on structuring your communication pipelines
    u
    • 2
    • 1
  • u

    uhe

    03/17/2017, 2:26 PM
    val channel = Channel<Int>()
        launch(CommonPool) {
            delay(50)
            println("Closing and flushing channel")
            channel.closeAndFlush()
        }
        (0..3).map { number ->
            launch(context) {
                println("Sending $number")
                channel.send(number)
                println("$number sent")
            }
        }.forEach { it.join() }
        println("Done!")
    which prints:
    Sending 0
    Sending 1
    Sending 2
    Sending 3
    Closing and flushing channel
    0 sent
    1 sent
    2 sent
    3 sent
    Done!
    e
    k
    • 3
    • 12
  • e

    elizarov

    03/17/2017, 3:24 PM
    Released
    kotlinx.coroutines
    version
    0.14
    with significant fixes related to reactive stream modules. Change log is here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/CHANGES.md
    👍 13
    r
    • 2
    • 2
  • j

    jmfayard

    03/18/2017, 9:00 AM
    I just gave a try to https://github.com/gildor/kotlin-coroutines-retrofit Very nice!
    g
    • 2
    • 2
  • d

    deviant

    03/18/2017, 2:52 PM
    are there any best practices how to debug coroutines/channels in idea/AS debugger? for example, how to determine the amount of currently running coroutines?
    e
    • 2
    • 1
  • k

    kingsley

    03/18/2017, 4:32 PM
    @elizarov given that using a job is the idiomatic approach to cancelling all running coroutines when a component is being destroyed, for instance. Should there be a method to cancel all child jobs but keep the parent job alive? Something like
    clear
    and
    cancel
    on a composite disposable in Rx I have a situation where the parent job should live longer than it's children.
    ➕ 1
    e
    d
    k
    • 4
    • 12
  • m

    moelholm

    03/18/2017, 9:00 PM
    did this example: https://github.com/nickymoelholm/smallexamples/blob/master/springboot-kotlin-playground/src/main/kotlin/com/moelholm/MessageClient.kt
    k
    s
    • 3
    • 4
  • d

    deviant

    03/19/2017, 3:20 PM
    hi guys! i'm going to implement coroutined version of
    list.forEachIndexed
    . i need the items order to be preserved, but each item should be processed in a separate thread (pool of 4 threads). is it a good idea to implement it via channels?
    e
    • 2
    • 1
  • d

    deviant

    03/19/2017, 8:35 PM
    https://www.philosophicalhacker.com/post/why-im-skeptical-about-kotlin-coroutines-for-android-development 🙈
    v
    • 2
    • 1
  • d

    deviant

    03/19/2017, 8:37 PM
    btw, i've just finished migration
    rx -> coroutines
    in one of my projects. totally happy so far 🙂
    👍 2
    k
    a
    • 3
    • 10
  • k

    kirillrakhman

    03/20/2017, 8:28 AM
    are inline suspend parameters planned? e.g. when I want to execute some suspend lambda but want to wrap it in some
    use
    style code but don't want additional allocations.
    e
    d
    • 3
    • 26
Powered by Linen
Title
k

kirillrakhman

03/20/2017, 8:28 AM
are inline suspend parameters planned? e.g. when I want to execute some suspend lambda but want to wrap it in some
use
style code but don't want additional allocations.
e

elizarov

03/20/2017, 8:30 AM
Yes. See https://youtrack.jetbrains.com/issue/KT-16603
👍 1
k

kirillrakhman

03/20/2017, 8:31 AM
thx
e

elizarov

03/20/2017, 8:31 AM
Also: https://youtrack.jetbrains.com/issue/KT-16448
k

kirillrakhman

03/20/2017, 8:32 AM
@elizarov actually, I was thinking of something slightly different:
inline fun launchWithDismiss(block: suspend () -> Unit) = launch(handleExceptions = true) {
    try {
        block()
    } finally {
        dismissAllowingStateLoss()
    }
}
is this also covered by the issue?
e

elizarov

03/20/2017, 8:37 AM
That works now if you add “noinline” modifier before the
block
param. Should also work without “noinline” modifier if the issue I’ve quoted is fixed.
I’m add to the comments
k

kirillrakhman

03/20/2017, 8:39 AM
There's no difference in allocations between
inline
with
noinline
and leaving out
inline
altogether, right?
e

elizarov

03/20/2017, 8:40 AM
Nope.
Btw, if all you want to do is to handle exception inside
launch
, then you don’t need it.
You need to define your own
CoroutineExceptionHandler
See here: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/
k

kirillrakhman

03/20/2017, 8:42 AM
I know, that's not what I want to do. What I want to do is dismiss a dialog, no matter how the coroutine terminates
ah I could do this with a context too, right?
e

elizarov

03/20/2017, 8:42 AM
You cannot do this with a context, but I think there is a better way to do it with coroutines:
val job = launch(context) { … doSomething ... }
launch(context + NonCancellable) { // !!! 
    job.await()
    dismissAllowingStateLoss()
}
k

kirillrakhman

03/20/2017, 8:44 AM
In which way is this better? allocation-wise? Or elegance-wise?
e

elizarov

03/20/2017, 8:44 AM
Elegance-wise, of course
k

kirillrakhman

03/20/2017, 8:45 AM
but isn't my code pretty readable itself?
e

elizarov

03/20/2017, 8:46 AM
Actually, my version does slightly different thing, which may make it nonappropriate for your needs
Your code invokes
dismiss
when the original code completes (including whatever other cleanup there was), while my code invokes
dismiss
as soon as the job is cancelled (it may invoke
dismiss
before
doSomething
code had terminated)
k

kirillrakhman

03/20/2017, 8:49 AM
yeah, I'll go with my version for now
d

deviant

03/20/2017, 9:25 AM
pretty dangerous. what if activity/fragment has been destroyed before job is done?
k

kirillrakhman

03/20/2017, 9:27 AM
@deviant this is a
DialogFragment
that has
retainInstance = true
the whole job of the class is to run some code no matter what the activity lifecycle
d

deviant

03/20/2017, 9:28 AM
ok then 🙂
k

kirillrakhman

03/20/2017, 9:29 AM
it's a pretty neat idiom, if I dare say so. the user has some visual feedback that some work is being done and I don't have to take care of lifecycle issues
View count: 3