https://kotlinlang.org logo
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
benchmarks
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
confetti
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
hiring-french
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
lincheck
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
Title
a

arnaud.giuliani

03/20/2019, 3:08 PM
Hello all, I have a question about
structured concurrency
. If I understand, the thing is to ensure that we reuse a proper
CoroutineScope
instead of launching coroutines in the `Global`f scope. Then here is my question. I have 2 components:
A
&
B
. My component
A
initiate a coroutine scope and I can run functions against that. How do I share/propagate it to my component
B
on which I want to call functions.
class A {
	val coroutineContext: CoroutineContext ...
}

class B {
	fun myFunction(){
		// run with A.coroutineContext?
	}
}
Do we have to pass the
coroutineContext
as an argument to any call of
B
?
b

bdawg.io

03/20/2019, 4:04 PM
“components A and B” is extremely vague in what the relationship between them is. It seems like you would make
A
implement
CoroutineScope
and then inject an instance of
A
into
B
class A : CoroutineScope {
    override val coroutineContext: CoroutineContext ...
}

class B(private val a: A) {
    fun myFunction() {
        a.launch { ... }
    }
}
CoroutineScope is just that, a scope for coroutines
g

gildor

03/20/2019, 4:06 PM
Also it's not clear why do you need scope or context in myFunction, but this is the key thing to answer on your question
a

arnaud.giuliani

03/20/2019, 4:07 PM
To give some precision
I start some work from A, in coroutine scope
this work can call a function on class B
and I have to keep my coroutine scope then
typically, A is an Android ViewModel that has it’s coroutine context
g

gildor

03/20/2019, 4:11 PM
What kind function on class B? Suspend, regular function, regular function that want to start new coroutine in scope A
a

arnaud.giuliani

03/20/2019, 4:11 PM
I don’t know well
I written suspend function until now
g

gildor

03/20/2019, 4:11 PM
And this is most important question
a

arnaud.giuliani

03/20/2019, 4:12 PM
B contains suspending functions
g

gildor

03/20/2019, 4:12 PM
Suspend function just runs, has nothing to do with scope, just call it from any coroutine
a

arnaud.giuliani

03/20/2019, 4:12 PM
yeah, but then the suspending functions just run in the current coroutine scope?
The thing is: from A to B I have mostly
suspending functions
g

ghedeon

03/20/2019, 4:14 PM
in will run in the ViewModel's scope (A). Isn't that what you want? And for dispatcher switch you have
withContext
. No need to pass CoroutineScope.
g

gildor

03/20/2019, 4:14 PM
Depends on what you mean by "run in the current scope", suspend function doesn't start new background job in scope, it just suspend and return when function return, so you don't need scope in general for suspend function, but suspend function will have context of coroutine from which it called, and most probably this coroutine created using scope context
Scope is required only for coroutine builders that run background tasks, like launch or async, to avoid asyncronous work leaking
a

arnaud.giuliani

03/20/2019, 4:16 PM
The is the thing
my work is mostly background
g

gildor

03/20/2019, 4:16 PM
But suspend function is not background
a

arnaud.giuliani

03/20/2019, 4:16 PM
The general case, I launch something from
A
and subcomponents called from
A
are suspending functions
g

ghedeon

03/20/2019, 4:16 PM
so far so good
a

arnaud.giuliani

03/20/2019, 4:16 PM
if
A
start a coroutines in background
suspending function should follow in this context if I understand
g

gildor

03/20/2019, 4:17 PM
Yes, suspend function has context of parent (call site) suspend function
a

arnaud.giuliani

03/20/2019, 4:18 PM
I have a corner case, when I need to create a channel to send data in an async way
g

gildor

03/20/2019, 4:18 PM
But context and suspend function is not about structured concurrency
👌 1
This is completely different case from suspend function
Depending how you create and use this channel
g

ghedeon

03/20/2019, 4:19 PM
For suspended functions: In A:
override val coroutineContext: CoroutineContext = job + Dispatchers.Main
In B:
suspend fun foo(){
   withContext(<http://Dispatchers.IO|Dispatchers.IO>){ some work }
}
For channels you'd need to pass the scope.
a

arnaud.giuliani

03/20/2019, 4:19 PM
currently in one suspending function, called from a coroutines
g

gildor

03/20/2019, 4:19 PM
If you use produce, which is coroutine + channel you should provide scope
a

arnaud.giuliani

03/20/2019, 4:20 PM
but then you are out of the initial scope?
g

gildor

03/20/2019, 4:20 PM
Why?
It's hard to recommend something without example of code
a

arnaud.giuliani

03/20/2019, 4:20 PM
yeah
let me try to gather a smll snippet
g

gildor

03/20/2019, 4:20 PM
If you use produce inside a scope it's fine, no need for additional scopes
👍 1
a

arnaud.giuliani

03/20/2019, 4:26 PM
I can’t use
produce
directly from a suspending function
I have to create it from a coroutineScope?
class A(val b : B) {
	
	val rootJob  = Job() 
    val coroutineContext = rootJob + <http://Dispatchers.dispatcherConfiguration.io|Dispatchers.dispatcherConfiguration.io>()

	fun runMe() = coroutineContext.launch {
        b.runMyChannel().consumeEach { myBooleanValue ->
        	// have incoming data
        }
    }
}

class B {
	
	suspend fun runMyChannel() = Channel<Boolean>{
		val channel = Channel<Boolean>()
		Global.async {
			// send a boolean value in a async way
			val myBooleanValue = true
			channel.send(myBooleanValue)
		}
		return channel
	}
}
How can I avoid using
Global.async
to send data in a async way?
g

gildor

03/20/2019, 4:32 PM
I can’t use
produce
directly from a suspending function
Yes, this is what I mentioned above, you need some scope
a

arnaud.giuliani

03/20/2019, 4:33 PM
Ok
g

gildor

03/20/2019, 4:33 PM
I don't see any reason why runMyChannel is suspend function
a

arnaud.giuliani

03/20/2019, 4:34 PM
yes here, not needed
g

gildor

03/20/2019, 4:34 PM
Just pass scope to this function argument and use it to launch produce coroutine
a

arnaud.giuliani

03/20/2019, 4:34 PM
Either, make
B
have its own scope?
perhaps not ... best to propagate scope to
B
to ensure that if we cancel
A
we cancel cal to
B
g

gildor

03/20/2019, 4:35 PM
Yes, this is one of the options, depends on case, but you have to manage lifecycle of B scope (cancel it on destroy)
a

arnaud.giuliani

03/20/2019, 4:35 PM
To give the complete story:
r

r4zzz4k

03/20/2019, 4:36 PM
Let me throw in a more specific example. If I want to run some coroutine builders, for example, from
onBindViewHolder
of
RecyclerView.Adapter
, I have to pass `Fragment`s scope to the adapter explicitly, right?
a

arnaud.giuliani

03/20/2019, 4:36 PM
yeah
To complete my story about B and why I need channels
in
B
I have old async API with callbacks
aka Firebase stuffs
r

r4zzz4k

03/20/2019, 4:37 PM
So something like
class MyAdapter(coroutineScole: CoroutineScope): RecyclerView.Adapter<...>(), CoroutineScope by coroutineScope {
should do?
g

gildor

03/20/2019, 4:37 PM
Also, side note, I'm not sure that understand code of your A class, it looks incorrect, it probably should implement CoroutineScope and you should just call launch as extension function in this scope, not on CoroutineContext, this wouldn't be available
a

arnaud.giuliani

03/20/2019, 4:38 PM
👍
r

r4zzz4k

03/20/2019, 4:38 PM
It's not incorrect, it's just explicit about it's context. IIRC, @louiscad has the same explicit scope in his Splitties. https://github.com/LouisCAD/Splitties/tree/master/modules/lifecycle-coroutines#example
g

gildor

03/20/2019, 4:38 PM
Yes, I also prefer style from Andrew's message with passing scope to constructor of a class that shares it with some other classes
a

arnaud.giuliani

03/20/2019, 4:39 PM
My sample is complex
because I want to catch data from an old async style API
into a channel
then into my old async callback, I have to recreate/reuse a coroutine scope to allow me to send data with my channel
g

gildor

03/20/2019, 4:40 PM
It's not incorrect, it's just explicit about it's context. IIRC
I'm not sure that it is the same tho, maybe, but it's not enough context to be sure
a

arnaud.giuliani

03/20/2019, 4:41 PM
user.getIdToken()
        .addOnCompleteListener { task ->
            // send my data here, but in other thread
        }
g

gildor

03/20/2019, 4:41 PM
Why not just write coroutines adapter for your call back based API, it would be easier to use and would not require additional complexity
a

arnaud.giuliani

03/20/2019, 4:42 PM
a coroutines adapter?
g

gildor

03/20/2019, 4:42 PM
Yes
Is it one time event or stream of events?
a

arnaud.giuliani

03/20/2019, 4:42 PM
yes
r

r4zzz4k

03/20/2019, 4:42 PM
Yeah, I agree. You just add a thin layer which wraps your callbacks into coroutines stuff and work with normal kotlinx.coroutines code in all the other app.
a

arnaud.giuliani

03/20/2019, 4:43 PM
that’s what I want ^^
r

r4zzz4k

03/20/2019, 4:44 PM
g

gildor

03/20/2019, 4:44 PM
And this (wrapping callbacks to coroutines) shouldn't involve any kind Structured concurrency machinery, scopes or contexts
a

arnaud.giuliani

03/20/2019, 4:45 PM
great guys 👍
yeah I understand, it’s a problem to continue the execution of a coroutine after a callback
and
kotlinx-coroutines-play-services
will clearly help 👍
👍 1
thanks
and the
Wrapping callbacks
also 👍
r

r4zzz4k

03/20/2019, 4:48 PM
Glad we could help, good luck with jumping on board 🙂
g

gildor

03/20/2019, 4:50 PM
Yes, play-services adapter does exactly this: wrapping callbacks, but a bit more complicated, also supports cancellation, converting multiple events to channel etc
r

r4zzz4k

03/20/2019, 4:54 PM
I'm not entirely sure there are channels specifically in play-services, but you can find an example of wrapping something channel-like here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/reactive/kotlinx-coroutines-rx2/src/RxChannel.kt You basically provide observer thing for the library you wrap, which either is a
Channel
itself or provides a channel where it emits events observed in upstream (and vice versa).
g

gildor

03/20/2019, 4:59 PM
Ahh, you right, for some reason I thought that Task supports multiple callbacks, maybe I thought about some other API
But careful with Rx adapter it's quite complicated
a

arnaud.giuliani

03/20/2019, 5:02 PM
mixing Rx/Coroutines + Firebase API ... yummy 😛
d

Dico

03/20/2019, 6:26 PM
class B {
    fun CoroutineScope.runMyChannel() = Channel<Boolean>().also { launch { it.send(true) } }
g

gildor

03/20/2019, 11:35 PM
This code can be replaced with produce,
b

bdawg.io

03/21/2019, 3:25 AM
@gildor except
produce
closes the channel when the
produce
coroutine returns
g

gildor

03/21/2019, 3:35 AM
Good point! But for example above it is probably good and more correct behaviour
d

Dico

03/21/2019, 4:04 AM
I assumed that the OP wanted to send more stuff to the channel @gildor
g

gildor

03/21/2019, 4:05 AM
Maybe, really hard to follow without actual use case
d

Dico

03/21/2019, 4:05 AM
True, given the example that was my assumption, didn't think it through very much.
I almost suggested just using
offer
😂 no CoroutineScope needed
a

Antanas A.

03/21/2019, 8:50 AM
@arnaud.giuliani, I've also struggled about understanding of scopes and jobs and relations between calls: Here is nice read about https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055 And my thread on github: https://github.com/Kotlin/kotlinx.coroutines/issues/1001 Short summary: there are two mechanisms in play: a) all coroutine builders (async, launch) will create new Job (coroutine) and attaches it into parent's job (parent is your current context where you've called async/launch unless you've explicitly passed new context with "context" parameter (applies only if passed context contains Job())) And these parent-child relationships plays a role on cancellation and join/waiting of coroutines. b) and there is "withContext" suspending behaviour, example it's just a suspend function. It suspends and waits on current coroutine where it have been invoked. withContext do also maintain parent-child the same as async/launch. So if "withContext" is used with explicit new context which contains Job() it gets more difficult to track because it suspends current coroutine and code block is launched on new coroutine which becomes a child of given Job(), it won't become a child of a coroutine where you've invoked from (they are tangled by suspend behaviour only)
👍 1
To answering directly to your question, if you have an object of class A in that object you can call launch/async and just call object's B any suspend functions and they are all be run on A's scope by default
unless these suspend function will contain some kind of switched e.g. withContext or launch(context =..)
Also take as a warning that default kotlin's implementation of withContext and launch do not maintains parent-child relations of callee so if you are explicitly switching context on new Job it won't be linked to your call graph, so if A's object scope will be cancelled all invocations deep inside call graph of withContext launch won't be cancelled (they are non-cancellable by default) (this applies only if you passing context with Job())
but if you are designing system there many objects maintains it's own scopes and want to forward executions of methods on these scopes then with current withContext / launch implementations you need explicitly implement cancellation tracking.
a

arnaud.giuliani

03/21/2019, 9:19 AM
@Antanas A. thanks 👍
a

Antanas A.

03/21/2019, 9:22 AM
np
g

gildor

03/21/2019, 9:23 AM
if A’s object scope will be cancelled all invocations deep inside call graph of withContext launch won’t be cancelled (they are non-cancellable by default
This is incorrect
withContext and any other suspend function is cancellable by default, if Job is cancelled any suspend point will interrupt execution of any suspend function
a

Antanas A.

03/21/2019, 9:24 AM
in theory only
g

gildor

03/21/2019, 9:24 AM
No
On practice
if you don’t have any blocking calls
only blocking calls are non-cancellable
a

Antanas A.

03/21/2019, 9:25 AM
sorry, maybe I've expressed wrongly
suspend function is cancelled, but a Job which was started by withContext is not cancelled by that, because it will be attached on some Job which you've passed with parameter in withContext(context = someNewContext) { codeblock() }
even if withContext, technically, it's suspension is cancelled
codeblock() will be run independently of that cancellation
g

gildor

03/21/2019, 9:27 AM
a Job which was started by withContext is not cancelled by that
withContext doesn’t start any Jobs, I don’t understand what you mean
a

Antanas A.

03/21/2019, 9:29 AM
wait, I'll try to explain by example
If I'm having a class A with CoroutineScope and it has coroutineContext = Dispatcher + job
and I'm doing withContext(context=coroutineContext) { }
Im passing a job
g

gildor

03/21/2019, 9:34 AM
yes, correct
if coroutineContext has a Job it will cancel withContext
I see what you mean, that if you pass another Job to withContext it will be non-cancellable
a

Antanas A.

03/21/2019, 9:36 AM
@test fun `withContext test`() = runBlocking { class A : CoroutineScope { val job = Job() override val coroutineContext: CoroutineContext get() = Dispatchers.Default + job suspend fun test() = withContext(coroutineContext) { while (isActive) { delay(1000) } println("work completed") } } val a = A() val job = launch { a.test() } delay(200) println("my jobs: " + job.children.toList()) println("A jobs: " + a.job.children.toList()) launch { delay(2000); job.cancel() // never cancels } job.join() // <- join is waiting also for test() to complete? But how? it's on different Job() println("done") // <- never reaching here }
here is example
my jobs: [] A jobs: ["coroutine#2":DispatchedCoroutine{Active}@1324409e]
it prints this
so withContext will start new coroutine (which has interface Job, thats why I'm calling sometimes job instead of coroutine)
So actually, I'm just passing my own context
g

gildor

03/21/2019, 9:37 AM
Yes, you can mess-up it, but why would you do this?
a

Antanas A.

03/21/2019, 9:37 AM
and it gets non cancellable by default
g

gildor

03/21/2019, 9:37 AM
Just because you replaced job
It’s nasty behaviour
a

Antanas A.

03/21/2019, 9:38 AM
I'm just thinking more like as Erlang objects
g

gildor

03/21/2019, 9:38 AM
but I still not sure about your point
why do you need to pass Job to withContext?
a

Antanas A.

03/21/2019, 9:38 AM
it contains it's own scope/context, and if I'm running some method on it I want that this object's scope will handle a load
I dont passing Job into with context I just passing my own context
as A class has CoroutineScope
so I just pass withContext(this.coroutineContext) { }
implementing coroutineContext of CoroutineScope without explicit job is unknown behaviour
g

gildor

03/21/2019, 9:40 AM
withContext(this.coroutineContext) what is use case for this?
a

Antanas A.

03/21/2019, 9:40 AM
all launches will be run on global scope then
g

gildor

03/21/2019, 9:40 AM
implementing coroutineContext of CoroutineScope without explicit job is unknown behaviour
What do you mean?
If you create CoroutineScope without job Job will be created automatically
a

Antanas A.

03/21/2019, 9:41 AM
if I'm implementing CoroutineScope on some class
then I'll definitely need some Job object to allow maintain a jobs launched on that scope
g

gildor

03/21/2019, 9:42 AM
Yes
and it has Job
a

Antanas A.

03/21/2019, 9:42 AM
without an explicit Job is not possible to track and cancel all launches
g

gildor

03/21/2019, 9:42 AM
It’s not true, you can get job from context
a

Antanas A.

03/21/2019, 9:42 AM
If I'm not explicitly pass a Job the Job object will be created each time new, then I "call Launch"
g

gildor

03/21/2019, 9:42 AM
or just cancel scope itself
a

Antanas A.

03/21/2019, 9:43 AM
Ok, one more demo
please wait a bit 🙂
by time, example is object DataDownloader : CoroutineScope, which has suspend fun download(url) = withContext(this.coroutineScope) { downloading } and method destroy() or cancelAll() that downloader can be used from may places in an application
and application must have a behaviour to track an cancel all downloads initiated in application
but also we need a way to cancel a specific download
g

gildor

03/21/2019, 9:45 AM
I really don’t understand why do you need this idiom
withContext(this.coroutineScope)
a

Antanas A.

03/21/2019, 9:45 AM
if a coroutine is cancelled which invoked a "download"
it has its own dispatchers
and these operations must be offloaded from original dispatchers
I mean all callees can be on any dispatchers
and DataDownloader should not be affected by that
g

gildor

03/21/2019, 9:46 AM
Yes, you switch to particular dispatcher using
withContext(MyDispatcher)
To cancel this downloading you just cancel job where this downloading started
a

Antanas A.

03/21/2019, 9:47 AM
If we've rewrite that code by this there will be no way to cancel all activities
from one place
yes, but it wont be possible to cancel everything at once
as I understood examples of android activities
the CoroutineScope was designed for that (to control lifecycle of coroutines of particular scope)
g

gildor

03/21/2019, 9:48 AM
I don’t understand your use case. suspend function that download something shouldn’t be bounded on any kind global state, it returns result or cancel
a

Antanas A.

03/21/2019, 9:48 AM
not a global state, but to a scope of downloader
why I cannot have a scope of downloader?
g

gildor

03/21/2019, 9:49 AM
I believe it’s just wrong implementation for this use case
a

Antanas A.

03/21/2019, 9:49 AM
looking as Erlang actors
g

gildor

03/21/2019, 9:49 AM
You may have, but than it shouldn’t be suspend function
a

Antanas A.

03/21/2019, 9:49 AM
but why not if it has only download operation
g

gildor

03/21/2019, 9:49 AM
rather Defered that runs coroutine on downloader
Or actor yes
Suspend function means that caller side manages lifecycle of this function
a

Antanas A.

03/21/2019, 9:50 AM
Maybe it's an edge case and really rare needs
but following logic I see no point why it not logical
g

gildor

03/21/2019, 9:51 AM
you of couse may add some trick to also control it from downloader, but you shouldn’t do that using Jobs, otherwise you get this nasty behaviour like in your first example
I think it’s abuse of cancellation that causes real problems with cancellation
a

Antanas A.

03/21/2019, 9:51 AM
but the pratice is if you are implementing CoroutineScope interface you should define job object otherwise it won't be possible to control scope
g

gildor

03/21/2019, 9:52 AM
but the pratice is if you are implementing CoroutineScope interface you should define job object otherwise it won’t be possible to control scope
Depending on how you do this
If you use
CoroutineScope()
or
MainScope
builders job will be created for your, no need to create it explictly
a

Antanas A.

03/21/2019, 9:52 AM
but Job will be created
SupervisorJob
g

gildor

03/21/2019, 9:53 AM
if you implementing interface than yes, you have to create Job yourself
a

Antanas A.

03/21/2019, 9:53 AM
and withContext(this.coroutineContext) { }
g

gildor

03/21/2019, 9:53 AM
Yes
a

Antanas A.

03/21/2019, 9:53 AM
will be non-cancellable immediately
g

gildor

03/21/2019, 9:53 AM
withContext(this.coroutineContext)
just wrong by definition IMO
I mean such idiom doesn’t have sense, I don’t see any valid usage
a

Antanas A.

03/21/2019, 9:54 AM
but why not
g

gildor

03/21/2019, 9:54 AM
I do not argue about Job, of course you need one implictly or explicitly created
a

Antanas A.

03/21/2019, 9:54 AM
or withContext is bad naming
withDispatcher
should be called instead
g

gildor

03/21/2019, 9:54 AM
No
a

Antanas A.

03/21/2019, 9:55 AM
but why illogical is to run on class's context
g

gildor

03/21/2019, 9:55 AM
see:
withContext(this.coroutineContext) {
   someSuspendCall()
}
and
someSuspendCall()
Is just the same
a

Antanas A.

03/21/2019, 9:55 AM
if I want to delegate execution on that
if called inside the same scope (class)
if someSuspendCall() is called from outer scopes eg Dispatchers.Main
g

gildor

03/21/2019, 9:56 AM
If you just want to create new scope use:
coroutineScope{}
instead of
withContext(this.coroutineContext
a

Antanas A.

03/21/2019, 9:56 AM
then its completely different semantics
g

gildor

03/21/2019, 9:56 AM
Yes, I see what you mean, you right
a

Antanas A.

03/21/2019, 9:56 AM
that scope will be bound to callee scope
g

gildor

03/21/2019, 9:56 AM
Sorry, now I got your case
Or not %)
a

Antanas A.

03/21/2019, 9:56 AM
😄
ok, imagine android app there everything is run on Main thread
g

gildor

03/21/2019, 9:57 AM
Yes, I see
a

Antanas A.

03/21/2019, 9:57 AM
some activities want to do downloading
g

gildor

03/21/2019, 9:57 AM
this.coroutineContext is actually CoroutineScope.coroutineContext
a

Antanas A.

03/21/2019, 9:57 AM
yes
g

gildor

03/21/2019, 9:57 AM
now I see
a

Antanas A.

03/21/2019, 9:57 AM
not the callees scope
I want to run away of callees scope actually
but maintain cancellation
because if activity is destroyed
g

gildor

03/21/2019, 9:58 AM
I still not sure that this is correct approach, because you mess up Job
a

Antanas A.

03/21/2019, 9:58 AM
I want to cancel that download
g

gildor

03/21/2019, 9:58 AM
Using dispatcher if fine
like
this.coroutineContext[CoroutineDispather]
a

Antanas A.

03/21/2019, 9:58 AM
but also I want somehow to cancel all suspended download() calls in system with another background operation which destroys and cancels downloader
g

gildor

03/21/2019, 9:58 AM
but using context together with job seems wrong for me
a

Antanas A.

03/21/2019, 9:59 AM
so all suspended functions will be cancelled
g

gildor

03/21/2019, 9:59 AM
but also I want somehow to cancel all suspended download() calls in system with another background operation which destroys and cancels downloader
I belive this is use case for Actor
a

Antanas A.

03/21/2019, 9:59 AM
yes, by doing this.coroutineContext[CoroutineDispather] it would work because new execution will be attached to callees parent
but I've no way to cancel by outside
from some background service downloader.cancelAllDownloadsImmediately()
maybe explicitly by adding to some list of jobs all created jobs
g

gildor

03/21/2019, 10:01 AM
Yes, and it’s good thing imo, but even if you need it, there are other ways to cancel, on level of your function itself, for example it easy to cancel all downloading tasks on Http client level
a

Antanas A.

03/21/2019, 10:01 AM
withContext(this.coroutineContext[CoroutineDispather]) { jobs += this.coroutineContext[Job] do stuff }
😄
g

gildor

03/21/2019, 10:01 AM
Yes, something like that
but It’s very nasty behaviour
It may cause terrible bugs
a

Antanas A.

03/21/2019, 10:01 AM
but why to do that if CoroutineScope is designed for that
scope is to group coroutines
g

gildor

03/21/2019, 10:02 AM
yes, but you try to group coroutines of one scope and coroutines of another scope
a

Antanas A.

03/21/2019, 10:02 AM
so suspend fun download() = withContext(this.coroutineContext) { }
even launch(this.coroutineContext) { } will corrupt parent-child
yes, but shouldn't better these also be tracked
and be cancelled
here and tell me if it's ok
g

gildor

03/21/2019, 10:05 AM
If I would need some global cancellation of all running tasks I would use async on my downloader or actor or other pattern
a

Antanas A.

03/21/2019, 10:05 AM
It's home made patch to maintain cancellation somehow
but I think it's better to have suspend behaviour instead of async
or are you suggesting
suspend fun download() = async(this@A.coroutineContext) { hob }.join()
?
g

gildor

03/21/2019, 10:06 AM
I will check discussion later
I see that Roman suggesting the same solution with launch/async
a

Antanas A.

03/21/2019, 10:06 AM
but async is also wont cancel children job (downloading)
g

gildor

03/21/2019, 10:07 AM
Why?
a

Antanas A.

03/21/2019, 10:07 AM
because one way or another it will be attached to coroutineScope's job
not a callees
g

gildor

03/21/2019, 10:08 AM
Yes, but this is caller responsibility to manage Defered if you need cancellation
a

Antanas A.

03/21/2019, 10:08 AM
but I'm not sure, I will test this solution
yes, but I'm talking about Downloader's responsibility to be allow cancel everything on downloader
all download tasks which is currently run
g

gildor

03/21/2019, 10:09 AM
But I agree, this is much more error prone
I would do this on Downloader implementation level rather than mess-up client cancellation flow
a

Antanas A.

03/21/2019, 10:09 AM
I just logically tried to implemented that downloader as thinking of objects which has a dispatcher and it's own scope
and by deferring all works with withContext(this@A.coroutineContext) { } looked as a way to go
but maybe it's just my logical thinking path which will be not followed by others
😄
just imagination that objects who has CoroutineScope is like a pools
where jobs can be run on
and be grouped together (and controlled)
independently of callers implementations and usages of these objects
I've suggestion that by default kotlin could maintain parent-child in any cases
unless NonCancellable object is passed into context
because now if anyone will pass context with a Job object it gets instant non-cancel behavior
Don't know exactly which is more appropriate and what behaviour is more frequent expected
d

Dico

03/21/2019, 1:19 PM
I suggested having multiple parents once but I didn't make a great case for it
Because I think it would make things really complex unnecessarily
And I suggested it alongside
withScope
function
a

Antanas A.

03/21/2019, 2:00 PM
I think multiple parents would be really overkill and more edge cases would appear.. I'd prefer solution to track a callee graph instead, but actually it would copy job child relations..
d

Dico

03/21/2019, 2:02 PM
Yeah, special extra parent
a

Antanas A.

03/21/2019, 2:03 PM
multiple parents is hard to understand, how about cancellation and supervision how to define rules which is which
d

Dico

03/21/2019, 2:03 PM
Exactly. That's my point
a

Antanas A.

03/21/2019, 2:04 PM
logically "extra parent" = "callee", and this property cannot be used for cancellation
because parent must cancel childrens
g

gildor

03/21/2019, 2:04 PM
But suspend function is not children imo
a

Antanas A.

03/21/2019, 2:04 PM
and if child knowns from what parent it originates
the parent itself couldn't find a children
yes, suspend fun is not children
everything is ok with suspend functions and logic behind
children is a new coroutine launched from some suspend function from another coroutine
now it's seems partly done, all works if coroutineContext[Job] is not changed
It feels that there are some missing concept - as CoroutineCreationGraph
on which cancellation could be implemented