Albert
03/29/2019, 2:46 PMSupervisorJob
to restart a coroutine when it stops exceptionally?
supervisorScope {
launch {
throw Exception()
}
}
Robert Jaros
03/29/2019, 5:52 PMJonathan Walsh
03/29/2019, 11:31 PMCoroutineStart.UNDISPATCHED
to help write CompletableFuture
alternate methods to my suspending functions for use by Java. But I see its marked as @ExperimentalCoroutinesApi
. Any recommendations / guidelines/ alternatives to using it? Or just be ready to uptake changes? Will post my specific example this threadHexa
03/30/2019, 12:10 AMraulraja
03/31/2019, 8:30 PMsuspend fun test(): Int = 0
What is the fastest possible way to run the function via startCoroutine
or similar in a blocking way to obtain 0
. This question is not specific to the coroutines-core lib but what I can do with the built in lang support for coroutines. I tried several conbinations with the EmptyCoroutineContext etc and using CountDownLatch
and was wondering if that is the the only way to run a suspended function in a blocking way without using runBlocking
octylFractal
04/01/2019, 12:18 AMlaunch
rocketraman
04/01/2019, 9:17 PMasync
block. I'm assuming this is the right way?
CoroutineScope(coroutineContext).async { ... }
jishindev
04/02/2019, 6:55 AMaddamsson
04/02/2019, 12:38 PMPersistent context for the coroutine. It is an indexed set of [Element] instances.
An indexed set is a mix between a set and a map.
Every element in this set has a unique [Key]. Keys are compared _by reference_.
is there a reason for not using a LinkedHashMap
?
what are the performance characteristics of an indexed set?addamsson
04/02/2019, 12:46 PMclass MyClass : CoroutineScope {
override val coroutineContext = Dispatchers.Default + SupervisorJob()
fun doSomething(): Job {
val handler = CoroutineExceptionHandler { _, exception ->
// ...
}
return launch(handler) {
// ...
}
}
}
what I want to achieve is that child jobs (launched with launch
) can fail independently. This works right now but it also works if I remove + SupervisorJob()
The docs say that if I add a CoroutineExceptionHandler
to a coroutine it won't bubble exceptions up which is fine but in SupervisorJob
I can see this:
A failure or cancellation of a child does not cause the supervisor job to fail and does not affect its other children,
so a supervisor can implement a custom policy for handling failures of its children:
A failure of a child job that was created using [launch][CoroutineScope.launch] can be handled via [CoroutineExceptionHandler] in the context.
so it implies that I can only use CoroutineExceptionHandler
in a SupervisorJob
yet it works without one. Did I miss something?Dico
04/03/2019, 6:35 AMCoroutineScope
in application components that use a coroutine lifecycle, favoring an approach using private val scope = CoroutineScope(...)
That way the scope is not leaked by implementing the interface and it is in my opinion cleaner. It distinguishes better between using the scope and using the scope of a coroutine you created. What do you think? Why should we implement CoroutineScope the way it is suggested initially by structured concurrency?Jacques Smuts
04/03/2019, 8:25 AMSlackbot
04/03/2019, 1:34 PMtseisel
04/04/2019, 9:31 AMsuspend fun
suspends until a certain condition is met ?
I tried the following :
runBlocking() {
val testSource = BehaviorProcessor.create<String>()
val subjectUnderTest = MyCache(coroutineScope = this, source = testSource)
val loading = async(start = CoroutineStart.UNDISPATCHED) { subjectUnderTest.getCachedValue() } // Suspend function call
assertFalse(loading.isCompleted)
// Sending an element to that processor satisfies the condition
testSource.onNext("Foo")
assertTrue(loading.isCompleted) // This fails : isCompleted is false
}
carlos cdmp
04/04/2019, 12:41 PMjw
04/04/2019, 12:43 PMVsevolod Tolstopyatov [JB]
04/04/2019, 1:25 PMkotlinx.coroutines
1.2.0-alpha-2 is here!
It has one major feature preview: cold streams aka Flow
to work with asynchronous cold streams of data.
The primary goal of the feature preview is to explore its possible strengths and weaknesses, evaluate real-world use-cases and finally adapt and refine it.
Try it, use it, break it!
Full API reference for `Flow`: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.htmlsdeleuze
04/04/2019, 5:21 PMkevinskrei
04/04/2019, 7:08 PM<http://GlobalScope.xxx|GlobalScope.xxx>
? For example
suspend fun sync() {
Realm.getDefaultInstance().use { realm ->
val variable = realm.getSomeVar()
val result = service.execute() //<- suspending function
val x = variable.y //<- Error because it returns to a different thread
}
}
Thank yousdeleuze
04/05/2019, 6:37 AMFlow
usable in multiplatform projects?Fredrik Larsen
04/05/2019, 7:30 AMsvenjacobs
04/05/2019, 10:09 AMJob
. When I add a Job
instance to a CoroutineContext
the behaviour of async
changes in regards to exception handling. Suddenly exceptions within async
are propagated to the default exception handler. On Android for example this causes the app to crash. try .. catch
around await()
don't help here. It seems that Job
adds the default exception handler to the context. However I need Job
so that I can cancel the context later. Here's an example (yes, it's Android but it's a generic problem, not related to Android)
class TestActivity : Activity(),
CoroutineScope {
override val coroutineContext: CoroutineContext = Dispatchers.Default + Job()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val deferred = async {
// App crashes here
throw NullPointerException()
}
launch {
try {
deferred.await()
} catch (e: Exception) {
Log.e("TestActivity", "ERROR", e)
}
}
}
override fun onDestroy() {
coroutineContext.cancel()
super.onDestroy()
}
}
How can I have the default behaviour of async
where exceptions are only thrown on await()
but also have the ability of a cancellable job in a context?louiscad
04/05/2019, 10:59 AMwithContext(<http://Dispatchers.IO|Dispatchers.IO>) { ... }
equivalent to coroutineScope { ... }
efficiency-wise if invoked from <http://Dispatchers.IO|Dispatchers.IO>
?Jacques Smuts
04/05/2019, 1:36 PMcmgurba
04/05/2019, 3:00 PMjob.cancel()
from this snippet in the coroutine docs, why does playground time out entirely? fun main() = runBlocking {
//sampleStart
val job = launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
delay(1300L) // delay a bit
println("main: I'm tired of waiting!")
job.cancel() // cancels the job
job.join() // waits for job's completion
println("main: Now I can quit.")
//sampleEnd
}
Paul Woitaschek
04/06/2019, 8:19 AMJakub Aniola
04/07/2019, 12:07 PMRuntime.getRuntime().availableProcessors()
allows. Is there any common rule how much I should handle this situation in kotlin coroutines?
Totally coroutines n00b heretseisel
04/07/2019, 1:52 PMFlow
is there, are channels still the right tool for wrapping callback APIs with `registerListener`/`unregisterListener` ? Looks like flowViaChannel
has been written for that purpose, but I don't see a way to "close" a Flow
like with channels and FlowablesPhilippe Boisney
04/07/2019, 3:14 PMNetworkBoundResource
with Coroutines (instead of Executor
like the Google sample https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/repository/NetworkBoundResource.kt). I also want that NetworkBoundResource
return a LiveData
through the method asLiveData()
.
1️⃣ My first try was to pass a CoroutineScope
to the constructor of NetworkBoundResource
, to properly manage cancellation.
Pros: in this way, tasks are canceled when a ViewModel is cleared for example.
Cons: My UserRepository
methods ask for parameter a CoroutineScope
for each call
🔗 https://gist.github.com/PhilippeBoisney/47b9eaee02df537cf7b0703707243a78
2️⃣ My second try was to use a GlobalScope
to launch the Coroutines inside the NetworkBoundResource
.
Pros: No more CoroutineScope
passed through `UserRepository`methods parameters.
Cons: GlobalScope
should be avoided (even if in Google sample, they use Executor
the same way of a GlobalScope
)
🔗 https://gist.github.com/PhilippeBoisney/4a5fc718a2b8d9735bdf2a5bfb37aebe
Which one of those approaches do you prefer? If none, how will you do? :thread-please:Sam
04/08/2019, 12:33 AMfun main() {
val handler = CoroutineExceptionHandler { _, throwable ->
println("Exception $throwable")
}
runBlocking {
val scope = CoroutineScope( coroutineContext + handler )
scope.launch {
throw Exception()
}.join()
}
println( "main done" )
}
Sam
04/08/2019, 12:33 AMfun main() {
val handler = CoroutineExceptionHandler { _, throwable ->
println("Exception $throwable")
}
runBlocking {
val scope = CoroutineScope( coroutineContext + handler )
scope.launch {
throw Exception()
}.join()
}
println( "main done" )
}
octylFractal
04/08/2019, 4:07 AMsupervisorJob
, child jobs propagate exceptions outwards: https://kotlinlang.org/docs/reference/coroutines/exception-handling.html#exceptions-in-supervised-coroutinesIf a coroutine encounters exception other than CancellationException, it cancels its parent with that exception. This behaviour cannot be overridden and is used to provide stable coroutines hierarchies for structured concurrency which do not depend on CoroutineExceptionHandler implementation. The original exception is handled by the parent when all its children terminate.
gildor
04/08/2019, 12:17 PMSam
04/08/2019, 1:05 PMval scope = CoroutineScope( handler )
gildor
04/08/2019, 1:25 PMSam
04/08/2019, 1:26 PMgildor
04/08/2019, 1:26 PMSam
04/08/2019, 1:28 PMval scope = CoroutineScope( Job() + handler )
gildor
04/08/2019, 1:28 PMSam
04/08/2019, 1:29 PMgildor
04/08/2019, 1:29 PMfun main() {
try {
runBlocking {
launch {
throw Exception()
}
}
} catch(throwable: Exception) {
println("Exception $throwable")
}
println( "main done" )
}
Sam
04/08/2019, 1:36 PMgildor
04/08/2019, 1:39 PMSam
04/08/2019, 2:30 PMgildor
04/08/2019, 4:42 PM