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
  • j

    Jarroyoesp

    09/24/2020, 3:42 PM
    Hi all, I am having some problems using KTor and coroutines in the iOS side. I am using 1.3.9-native-mt-2 but I can't create Ktor-HttpClient in the iOS side. I don't know if I am missing something. My dependencies are:
    kotlinVersion = "1.4.0"
    ktorVersion = "1.4.0"
    coroutineVersion = "1.3.9-native-mt-2"
    serializerVersion = "1.0.0-RC"
    
    internal actual val ApplicationDispatcher: CoroutineContext  =
        NsQueueDispatcher(dispatch_get_main_queue())
    
    internal class NsQueueDispatcher(
        private val dispatchQueue: dispatch_queue_t
    ) : CoroutineDispatcher() {
        override fun dispatch(context: CoroutineContext, block: Runnable) {
            dispatch_async(dispatchQueue) {
                block.run()
            }
        }
    }
    But when I try to create HttpClient this exception is thrown(
    kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.ChildHandleNode
    ):
    private val client by lazy {
         HttpClient() {
             install(JsonFeature) {
                 serializer = KotlinxSerializer(nonStrictJson)
             }
             install(Logging) {
                 logger = Logger.DEFAULT
                 level = <http://LogLevel.INFO|LogLevel.INFO>
             }
         }
    }
    Has anyone had the same problem? Thanks in advance.
    r
    • 2
    • 3
  • a

    Andrea Giuliano

    09/24/2020, 5:09 PM
    Hi guys, I was playing with
    withTimeout
    function and I’ve noticed that if the block inside is not a suspending function, withTimeout simply does nothing. Is that the expected behaviour? I know from the spec it’s stated that the block is a suspending block. However I was expecting at least some error or warning if the block is not suspending. Anything I’m missing here?
    s
    l
    • 3
    • 5
  • c

    cueball

    09/24/2020, 6:22 PM
    Hi guys, I’m struggling with a piece of code while trying to use coroutines. I’m calling Spring’s
    reactive.function.client
    method
    awaitBody()
    from a Generic class and am running into error:
    Kotlin: Cannot use 'Response' as reified type parameter. Use a class instead.
    ---
    class ReactiveWebClient<Request: Any, Response: Any> {
    
        suspend fun post(uri: URI, request: Request, headers: (HttpHeaders) -> Unit): Response {
            return <http://webClient.post|webClient.post>()
                .uri(uri)
                .body(BodyInserters.fromValue(request))
                .headers(headers)
                .retrieve()
                .awaitBody()  // error on this line
        }
    }
    Signature of `awaitBody`:
    public suspend inline fun <reified T : kotlin.Any> org.springframework.web.reactive.function.client.WebClient.ResponseSpec.awaitBody(): T { /* compiled code */ }
    Can someone please help? I’m not so well versed with Kotlin’s type parameters.
    d
    u
    • 3
    • 4
  • n

    Nick

    09/24/2020, 8:54 PM
    My teammates said that I can’t use
    callbackFlow
    and
    flatMapMerge
    because they are marked as
    @FlowPreview
    and
    @ExperimentalCoroutinesApi
    . Does anyone have any idea when we can expect these to become stable?
    t
    j
    +3
    • 6
    • 26
  • r

    ribesg

    09/25/2020, 10:25 AM
    Nobody knows how to prevent freeze propagation in coroutines? Like, here the
    http.get
    will freeze the coroutine it runs in and I’m trying to prevent it from propagating outside
    coroutineScope { }
    but my entire codebase still gets frozen...
    override suspend fun getUsers(): List<User> {
            val http = http.freeze()
            val url = "$BASE_URL$USERS_PATH".freeze()
            
            val result =
                coroutineScope {
                    http.get<List<User>>(url)
                }
    
            return result
        }
    How do you properly isolate such call? I’ve been trying random things for days
    b
    l
    e
    • 4
    • 13
  • c

    Casey Brooks

    09/25/2020, 3:38 PM
    Is it possible to write tests that verify that a suspend method is running on a particular dispatcher? For example, testing that a service call is executed within a
    withContext(ioDispatcher)
    block (
    ioDispatcher
    is injected and is a
    TestCoroutineDispatcher
    in unit tests)
    j
    • 2
    • 1
  • f

    Florian

    09/26/2020, 7:55 AM
    I use Channels to send one-off events from my ViewModel to my fragment. What should I use for events that should only be received once at most? ConflatedChannel for some reason still allows multiple events to be sent. Right now I have this: (
    receive
    suspends till an event is sent over the channel, but the return type is
    Unit
    so I ignore the return value)
    g
    • 2
    • 21
  • a

    Allan Wang

    09/26/2020, 8:11 AM
    How do we cancel flow collections before all items are collected? Should we throw a cancellationexception like the internal
    collectWhile
    function?
    g
    • 2
    • 1
  • f

    Florian

    09/26/2020, 8:22 AM
    Is it sufficient to use 
    viewModelScope
     for database operations (Room)? Theoretically, it could be canceled prematurely if we leave the app before the operation is done (although I don't know if it's possible to leave the app that fast), right? Do you do anything specific to prevent this?
    g
    • 2
    • 14
  • a

    Allan Wang

    09/26/2020, 9:57 AM
    Is it acceptable to use
    withContext(CoroutineExceptionHandler { … }) { … }
    , or are exception handlers only usable in
    launch
    and
    async
    ?
    c
    t
    • 3
    • 2
  • s

    spierce7

    09/26/2020, 8:41 PM
    If I have a resource inside a
    Flow
    , what’s the best way to call something when the context it’s running in is cancelled? i.e. when the flow is cancelled, I need to call `process.destroy()`:
    actual fun run(command: String): Flow<String> = flow {
            val split = command.split(' ')
            val process = ProcessBuilder(split).apply {
                redirectErrorStream(true)
            }.start()
    
            process.inputStream.source().use { processSource ->
                processSource.buffer().use { bufferedProcessSource ->
                    while (true) {
                        val line = bufferedProcessSource.readUtf8Line() ?: break
                        emit(line)
                    }
                }
            }
        }.flowOn(<http://Dispatchers.IO|Dispatchers.IO>)
    b
    • 2
    • 7
  • f

    Florian

    09/27/2020, 7:34 AM
    This is from Google's sample. I'm particularly interested in the
    _taskUpdatedEvent
    , which triggers the back navigation in the fragment. They navigate back from the screen after the insert operation is finished. Isn't this pretty similar to blocking the UI thread? It won't freeze the UI, but you can't actually do anything on that screen until the operation is finished and it's not really necessary to not navigate back immediately. I know that this operation will only take a few milliseconds but still, there is a reason why we execute queries on a background thread in the first place. Also, this could take longer depending on what happens in the repository. What is your opinion? Edit:
    createTask
    and
    updateTask
    are suspending functions
    s
    • 2
    • 15
  • a

    Allan Wang

    09/27/2020, 10:16 AM
    For flows, we can collect for x seconds by doing
    withTimeoutOrNull(time) {
      flow.collect { ... }
    }
    Is there a way to do this in the middle of the flow, such that the function still returns
    Flow<T>
    ?
    c
    b
    • 3
    • 4
  • a

    Animesh Sahu

    09/27/2020, 1:04 PM
    Any tips on testing the Flow from a ConflatedBroadcastChannel? Seems like this test hangs,
    val ch = BroadcastChannel<Int>(Channel.CONFLATED)
    var counter = 0
    ch.asFlow().onEach { ++counter; yield() }.launchIn(this)
    ch.offer(150)  // send random element
    yield()
    
    counter shouldBeExactly 1
    this
    in launchIn() is TestContext of kotest framework: https://github.com/kotest/kotest (probably single threaded) but I assume yielding will change the coroutine to the waiting coroutine, right?
    f
    • 2
    • 2
  • n

    Nemanja Scepanovic

    09/28/2020, 7:55 AM
    Hi guys, Is CompletableDeferred’s await method cooperative, meaning if wrapping context gets
    CanellationException
    would this completable deferred’s job be automatically completed or not? If not, what is the best way of making it cooperative? Thanks 🙂
    d
    a
    • 3
    • 9
  • d

    Daniele Segato

    09/28/2020, 9:05 AM
    This question is out of curiousity, I do not currently need this. Is there a reentrant lock (mutex) for kotlin coroutines? If not why? Couldn't the context / parent context act as the reentrant discriminant? (if a parent scope hold the lock a children can enter it) Previous question (which basically explain what a reentrant lock is): Is there something in coroutines that works as a context-aware mutex in a way similar to
    synchronize
    keyword? Synchronize lock access to the thread level. This means that if you call another fun that use the synchronize on the same object without jumping threads that fun doesn't block at the synchronize keyword. With coroutines if you use a mutex in 2 fun and one call the other it creates a deadlock. Is there something like a mutex that allow "children" coroutines of the lock holder go into the lock?
    w
    • 2
    • 2
  • a

    Allan Wang

    09/29/2020, 8:53 AM
    Is there a difference between
    launch { … }.join()
    and
    coroutineScope { }
    ?
    g
    z
    • 3
    • 4
  • p

    Pablo

    09/29/2020, 12:29 PM
    I'm not using 
    viewModel
     from now, so I don't have the coroutineScope on my 
    presenter
     and one way I'm doing right now is I'm injecting with 
    Dagger
     a 
    CoroutineScope
     as follows :
    @Module
    object CoroutineMainScopeModule{
      @Provides
      fun provideCoroutineMainScope() : CoroutineScope : MainScope() 
    }
    But I don't know if I'd have problems in the future, cna someone let me know the disvantages of doing that way? I also don't want to implement 
    CoroutineScope
     on the presenter because I read it's a bad praxis, neither use 
    GlobalScope
    . I'm afraid because I don't have a 
    Job
     there, well I mean yes, I have a 
    ContextScope(SupervisorJob() + Dispatchers.Main)
     but in case I wanted to do things with different jobs, would be a problem there?
    a
    g
    • 3
    • 7
  • m

    mp911de

    09/29/2020, 1:55 PM
    And we’re not sure which way is the better for the Coroutine API variant:
    // variant 1: suspendable
    val api: RedisClusterSuspendableCommands<String, String> = connection.suspendable()
    
    // variant 2: coroutine
    val api: RedisClusterCoroutineCommands<String, String> = connection.coroutine()
    t
    • 2
    • 1
  • t

    Tim Malseed

    09/30/2020, 5:08 AM
    I have a flow, and I’d like to keep track of ‘progress’ (I know the flow will complete). I’m wondering if there’s a better model than this:
    fun findSongs(): Flow<Pair<Song, Int>>
    Where the flow emits a song, and a count of the total number of songs.. It seems a little redundant to have to emit the same ‘total’ each time. But the alternative is to have a separate function for retrieving the count, and thats’ not particularly efficient..
    c
    t
    a
    • 4
    • 21
  • s

    spand

    09/30/2020, 9:28 AM
    I am trying to find an equivalent pattern for
    by lazy
    variable with coroutines. I can call
    async(start = Lazy){ expensiveCalculation }
    but afaik it does not make the computation optional, only delayed. What am I missing?
    r
    l
    g
    • 4
    • 28
  • p

    Pablo

    10/01/2020, 7:21 AM
    What is the difference between do
    TestCoroutineScope.runBlockingTest{}
    And
    TestCoroutineDispatcher.runBlockingTest{}
    I see some people use them, and others just put
    runBlockingText{}
    without any dispatcher nor coroutineScope, is there any difference?
    g
    • 2
    • 12
  • p

    Pablo

    10/01/2020, 7:26 AM
    Also on 2019 on Android Dev Summit '19 said that we should ALWAYS inject Dispatchers to make the dispatcher different from production as in the unit test. Is still like this?
    g
    • 2
    • 22
  • p

    Pablo

    10/01/2020, 7:32 AM
    Because if it's like this, if for instance I have a CoroutineDispatcher injected that is the IO and the CoroutineScope tha tis the
    MainScope()
    and I use it like :
    @Inject constructor(
    ...
    ...
    private val mainScope : CoroutineScope,
    private val ioDispatcher: CoroutineDispatcher,
    ...
    ...
    )
    And then using it like this
    mainScope.launch{
      ...
      result withContext(ioDispatcher) {...}
      ...
    }
    If I do
    Dispatchers.setMain(TestCoroutineDispatcher())
    does it change also the
    ioDispatcher
    ? On my test I'm doing it like this :
    private val testDispatcher = TestCoroutineDispatcher()
    private val mainScope = TestCoroutineScope(testDispatcher)
    And then on the
    @Before
    I'm using this
    Dispatchers.setMain(testDispatcher)
    And on every test I'm using
    mainScope.runBlockingTest{...}
    or
    testDispatcher.runBlockingTest{...}
    Is something I could improve?
    g
    • 2
    • 12
  • s

    Se7eN

    10/01/2020, 5:29 PM
    Can I make this code better?
    templatesDao.getAll().collectIndexed { index, templates ->
    
        templatesLiveData.value = templates
        if(index == 0) {
            val newTemplates = getTemplatesFromFirestore(templates.lastIndex)
            templatesDao.insert(newTemplates)
        }
    }
    Is there any function like
    flow.onFirst { }
    or something like that
    a
    t
    • 3
    • 3
  • a

    ansman

    10/02/2020, 12:13 AM
    I'm implementing a custom
    CoroutineDispatcher
    that is an immediate behavior. I noticed that
    dispatchYield
    is marked with
    @InternalCoroutinesApi
    . Can someone confirm that this is there to prevent people from calling the method, not implementing it? Reading the documentation it seems that this is meant for custom dispatchers to implement but that you should avoid calling it manually
    e
    • 2
    • 6
  • b

    Big Chungus

    10/02/2020, 7:42 AM
    Could anyone point me to some articles to get me started with flows, please?
    :google: 2
    f
    k
    • 3
    • 2
  • p

    Pablo

    10/02/2020, 10:12 AM
    Is flow(retry or retryWhen ) good in a case that we are waiting for a backend response until the response is ok? Let's say if we do 3 calls and is failure then error to the UI (Android) but if the first KO second KO and the thirth is OK return OK. Any other idea using Retrofit2?
    t
    • 2
    • 5
  • s

    Shashank

    10/02/2020, 11:21 AM
    How are you testing
    StateFlow
    ? If I have a data class
    data class ProfileState(val isLoading: Boolean, val userName: String?)
    and I have the following states which are being emitted 1. ProfileState(true, null) 2. ProfileState(false, “John Doe”) So how do I that I received the states in the correct order while accounting for the conflation which is done by
    StateFlow
    ?
    j
    t
    • 3
    • 9
  • i

    Isaac

    10/02/2020, 5:29 PM
    Does anyone have a workaround for this?
    r
    v
    • 3
    • 5
Powered by Linen
Title
i

Isaac

10/02/2020, 5:29 PM
Does anyone have a workaround for this?
r

Rechee Jozil

10/02/2020, 9:49 PM
This isn't specific to Coroutines it seems. Post to android channel?
i

Isaac

10/02/2020, 10:54 PM
I have
r

Rechee Jozil

10/03/2020, 12:05 AM
@Isaac then I say if you posted in two places no responses your question needs to be better. Post the code
➕ 1
Also, there's a lot of places this can go wrong. Is it the fragment... Is it flow... Is it recycler view
v

Vipulyaara

10/04/2020, 9:10 AM
what do you mean adds the deleted image? wasn't the image there already in Fragment1? Keep in mind that deleting data will send a notification to any observers of that table. So if Fragment1 had some query on this table, it will get notified when you delete the image.
View count: 3