prasham.h.trivedi
01/10/2019, 7:17 AMwithTimeOut
construct doesn’t throw TimeOutCancellationException
?
In my android service I am using combination of timeout + channels to send data to specified duration using channels, and the sending operation should end after specified time. I have observed that channels kept sending the data well past timeout limit.
Here is my pseudocode
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
val openChannel = openChannel(interval = interval)
try {
withTimeout(timeMillis = duration) {
openChannel.consumeEach {
doOnTick()
}
}
} catch (e: TimeoutCancellationException) {
openChannel.cancel()
stopService()
}
}
In this code catch
isn’t executed sometimes.
This code runs properly on kotlin playground, what is wrong above?simon.vergauwen
01/10/2019, 11:36 AMUnconfined
& EmptyCoroutineContext
?matt tighe
01/11/2019, 7:07 PM@Test
fun `Submits app selection if selections can be made`() {
mainActivityViewModel.submitAppSelection(selectedApp)
runBlocking {
verify(mockAppsStartupFsm).submitEvent(AppSelected(selectedApp))
}
}
class MainActivityViewModel {
fun submitAppSelection(app: App) {
if (!selectionsCanBeMade()) return
lastSelectedApp = app
val coroutineScope = CoroutineScope(Dispatchers.Default)
coroutineScope.launch { appsStartupFsm.submitEvent(AppSelected(app)) }
}
}
AppsStartupFsm#submitEvent
is a suspending function.
Changing the runBlocking scope to be function level doesn’t seem to help. Is there something I’m missing with verification of suspending functions?groostav
01/11/2019, 10:04 PMsubmitEvent
callSaiedmomen
01/12/2019, 12:21 AMException in thread "main" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
Does anyone know Dispatchers.setMain
with what parameter should be called?Saiedmomen
01/12/2019, 12:45 AMSaiedmomen
01/12/2019, 9:18 AMzjuhasz
01/13/2019, 5:41 AMhmole
01/13/2019, 2:09 PMThreadLocal.asContextElement
? Currenlty I use retrofit with CompletableFuture
futures and then using async
extension function from jdr8 integration package. But the request itself is executed in OkHttp own thread pool and so my ThreadLocal
is null there, since coroutine doesn't know about this thread.ghedeon
01/13/2019, 7:24 PMNetworkBoundResource
from Google's app guide: https://developer.android.com/jetpack/docs/guide#addendum. How do you replicate similar functionality in coroutines world? Can you reuse it? Do you apply some completely different technic?
Some thoughts:
Case 1: network request, no caching required:
Before: return LiveData<Resutl>()
After: probably just suspended get(): Result
? Seems enough. Error/Success
, no need for a stream here.
Case 2: network request, return cache first, update cache, return fresh data
Before: return NetworkBoundResource
as LiveData (emits two items, cached and refreshed)
After? Channels? Actors?
(P.S. Does anybody have an example of cache aware Repository with coroutines? )Allan Wang
01/13/2019, 10:12 PMthana
01/14/2019, 7:46 AMmarcinmoskala
01/14/2019, 8:14 AMAlexjok
01/14/2019, 8:32 AMdave08
01/14/2019, 4:19 PMcoroutineScope { }
that contains it, it works... why should it work that way?maxmello
01/14/2019, 4:25 PMfun doIt() = runBlocking { value = withContext(Dispatchers.Default) { // some calculations } }
vs just doing everything on the main thread? As I understand, runBlocking
will still block the main thread (if doIt
is called from it), is there any practical difference between blocking the main thread by runBlocking
and by just doing some calculations on the main thread?ghedeon
01/15/2019, 9:13 AM@Test
fun `Channel exception error handler`() {
val errorHandler = CoroutineExceptionHandler { _, throwable ->
assertThat(throwable.message).isEqualTo("Failure")
}
runBlocking(errorHandler) {
val channel = produce<Nothing> { close(Exception("Failure")) }
channel.consumeEach { println() }
}
}
leandrodev
01/15/2019, 11:52 AM4.7
? https://github.com/Kotlin/kotlinx.coroutines/tree/master/native
My problem is that with the new android plugin version 3.3.0
requires at least a gradle version of 4.10.1
and it got complicated to update my mppmaxmello
01/16/2019, 10:41 AMGlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) { synchronized(lock) { // some operations } }
potentially worse than thread { synchronized(lock) { // some operations } }
?Dalinar
01/16/2019, 4:24 PMnikospamp
01/16/2019, 7:03 PMoctylFractal
01/16/2019, 9:21 PMReceiveChannel<ByteReadPacket>
but no way to turn that into an InputStream
for a Java API. I don't care if the InputStream
blocks instead of suspending, but I'd like keep it in coroutines until I hit JavaAllan Wang
01/17/2019, 6:58 AMscope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
val result = runCatching {
fetcher(key)
}
receiverChannel.send(key to result)
}
The scope is currently GlobalScope
and fetcher
is a suspended function (that just returns an exception)
Even with runcatching, I’m getting
FATAL EXCEPTION: DefaultDispatcher-worker-4
...
java.lang.RuntimeException: Test
Is there something else I’m missing? Do I need to add a new scope here?ghedeon
01/17/2019, 10:03 AMfun CoroutineScope.foo(): ReceiveChannel<Int> = produce {}
What if it's in a different class? Then the usage is ... strange? 🙂
class Bar{
fun CoroutineScope.foo(): ReceiveChannel<Int> = produce {}
}
.... coroutine scope ....
bar().run {
foo() ??
}
ghedeon
01/17/2019, 10:06 AMproduce
is the way to go. Should I just pass CoroutineScope
as a parameter?ghedeon
01/17/2019, 11:34 AMReceiveChannel.asLiveData()
implementations?dave08
01/17/2019, 2:57 PMmapNotNull
...? It might be nice to add such extensions in kotlinx.coroutines that take suspend lambdas no (I don't know how this would work though)? Or use inline functions...A Gamal
01/17/2019, 4:45 PMTestCoroutineContext
. In unit testing, should I init it in classes that have CoroutineContext
in the constructor?hohnjogan
01/17/2019, 10:23 PMmingkangpan
01/18/2019, 9:01 AMcoroutineScope(UI) {
try {
val a = async(IO) {
delay(1000)
println("Done after delay")
}
val b = async(IO) { throw Exception() }
awaitAll(a, b)
} catch (e: Exception) {
println("Caught $e")
}
}
^ why does this crash my application?
why is the exception not propagated to the outer try & catch
?mingkangpan
01/18/2019, 9:01 AMcoroutineScope(UI) {
try {
val a = async(IO) {
delay(1000)
println("Done after delay")
}
val b = async(IO) { throw Exception() }
awaitAll(a, b)
} catch (e: Exception) {
println("Caught $e")
}
}
^ why does this crash my application?
why is the exception not propagated to the outer try & catch
?gildor
01/18/2019, 9:02 AMmingkangpan
01/18/2019, 9:03 AMgildor
01/18/2019, 9:03 AMmingkangpan
01/18/2019, 9:13 AMgildor
01/18/2019, 9:15 AMlouiscad
01/18/2019, 11:08 AMtry {
coroutineScope {
val a = async(<http://Dispatchers.IO|Dispatchers.IO>) {
delay(1000)
println("Done after delay")
}
val b = async(<http://Dispatchers.IO|Dispatchers.IO>) {
throw Exception()
}
awaitAll(a, b)
}
} catch (e: Exception) {
println("Caught $e")
}
mingkangpan
01/18/2019, 11:13 AMscope(IO) {
async { }
async { }
}
vs
scope {
async(IO) { }
async(IO) { }
}
gildor
01/18/2019, 11:14 AMmingkangpan
01/18/2019, 11:54 AMgildor
01/20/2019, 11:38 AM