etolstoy
09/16/2021, 3:54 PMkotlinx.coroutines.
prefix) will not be shown unless you click on <3 internal lines>
. After one clicks on this caption — all the underlying folded lines will be shown to the user.
Folded code:
at my.business.Logic.f(Logic.kt:30)
at my.business.Logic.g(Logic.kt:150)
at my.business.Logic.main(Logic.kt:99) <3 internal lines>
at not.mine.but.also.BusinessLogic.main$1(BusinessLogic.kt:20)
at not.mine.but.also.BusinessLogic.main(BusinessLogic.kt:19)
Unfolded code:
at my.business.Logic.f(Logic.kt:30)
at my.business.Logic.g(Logic.kt:150)
at my.business.Logic.main(Logic.kt:99)
at kotlinx.coroutines.invokeAsync(Coroutines:999)
at kotlinx.coroutines.applySuspend(Coroutines:18)
at kotlinx.coroutines.runBlocking(BlockingCoroutines:555)
at not.mine.but.also.BusinessLogic.main$1(BusinessLogic.kt:20)
at not.mine.but.also.BusinessLogic.main(BusinessLogic.kt:19)
To understand the possible consequences on user experience we want to know how you feel about seeing or not seeing coroutine internal stuff in your stacktraces (please note that they will be still available via clicking on <N lines folded>). And if you need them, it would be interesting to understand your specific use cases. Please, share your thoughts in the thread.Vikas Singh
09/16/2021, 4:42 PMNorbi
09/17/2021, 10:05 AMCancellationException
? Eg.:
suspend fun etc() {
try {
someOtherSuspendFun()
} catch (e: Exception) {
if (e is CancellationException) {
throw e // The operation has also failed but I should not throw WrapperException because it will interfere with the cancellation mechanism
} else {
throw WrapperException("Operation failed", e)
}
}
}
(The above is pseudo code, I have not tried it.)
Thanks.ribesg
09/17/2021, 11:26 AMPHondogo
09/18/2021, 7:06 AMFudge
09/18/2021, 8:21 AM.collect {}
call of a MutableStateFlow
not work after the initial value?
Basically, I create a MutableStateFlow
in one place and call emit
(multiple times) there, and at the same time call .collect {}
in another place, and the callback of collect
is not executed, except for the initial value. I can clearly see the state of the Flow
is changing, but collect
is just not working.
I'm also absolutely sure the collect
call is not getting canceled, as I have tried catching CancellationException
.
The context is a Jetpack Compose app, maybe it's some bug there?
SOLVED: the problem was emitting the same list to the StateFlow. Even though the list is changing, its reference has not, and coroutines treat it as the same value, and concludes no new value needs to be emitted. Hence collect
callback is not called after the initial value.Chachako
09/19/2021, 12:17 PMlaunch { send(..) }
in produce {}
and ensure I can receive them in produce.consumeEach
?K Merle
09/20/2021, 8:32 AMpreviousBackStackEntry
?
It's always null in case:
val navController: NavHostController = rememberNavController()
val previousBackStackEntry = navController.previousBackStackEntry
and works correctly when has these 2 extra lines:
val navController: NavHostController = rememberNavController()
val currentState by navController.currentBackStackEntryAsState()
val currentRoute: String? = currentState?.destination?.route
val hasPreviousBackStackEntry = navController.previousBackStackEntry
Xenex
09/20/2021, 2:56 PMCLOVIS
09/20/2021, 7:34 PMOrhan Tozan
09/21/2021, 1:59 PMFlow.debounce()
and I saw it's quite complex. Now thinking about it, can't debounce be simplified to the following implementation?:
fun Flow.debounce(timeoutMillis: Long) = transformLatest {
delay(timeoutMillis)
emit(it)
}
ianbrandt
09/21/2021, 4:20 PMSharedFlow
and StateFlow
under https://kotlinlang.org/docs/? If so, is there a related YouTrack/GitHub issue that could be followed and contributed to?
Not that the KDocs aren't good, but it seems like a rather important component of the API, and I'm not seeing any mention of it on the #kotlin-website at all.leandro
09/22/2021, 7:40 PMbrabo-hi
09/22/2021, 11:07 PMval players: Flow<List<HockeyPlayer>> =
playerQueries.selectAll()
.asFlow()
.mapToList()
players,collect{
// i am not getting new list when item is added, i only receive data once
}
What could i be doing that is not right ?Scott Christopher
09/23/2021, 10:31 AMlaunch
but don't want to dictate which CoroutineScope it is attached to, so I have created it as an extension method to CoroutineScope
so launch
can be called against it. This approach feels like it is guarded pretty heavily against through the use of coroutineScope
and friends - is there a more idiomatic way I should be spawning new tasks in the context of the calling function, or is this approach considered okay?mkojadinovic
09/24/2021, 12:36 PMprivate suspend fun getSomething() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
RestTemplate().exchange(
"url", <http://HttpMethod.POST|HttpMethod.POST>, HttpEntity("body"), String::class.java
)
}
Will this produce many threads to wait, or I am safe that this will use some limited thread pool? What if I want to limit thread pool size to 2, will it also work?Paul Woitaschek
09/24/2021, 12:43 PMObservable.never()
?Mike Speed
09/24/2021, 1:22 PMprivate suspend fun getThing(){...}
How can I set a minimum return time on this? So it will only return if at least 2000ms have passed, for example?Michael Strasser
09/26/2021, 11:48 AMChannel
, returning a list of items after timeoutMs
milliseconds (the list might be empty). Here is the code:
public suspend fun <E> receiveBatch(
channel: ReceiveChannel<E>,
timeoutMs: Long,
): List<E> {
val batch = mutableListOf<E>()
withTimeoutOrNull(timeoutMs) {
for (item in channel) {
batch.add(item)
}
}
return batch
}
In simple testing everything is fine, but under load sometimes an item is not added to a list and is lost. What I think happens is that withTimeoutOrNull
times out between receiving an item from the channel and adding it to the list.
Does anyone have a solution for this issue, or a better design for what I am trying to do?Stylianos Gakis
09/26/2021, 6:04 PMAlbert
09/27/2021, 8:03 AMwithout_actor
runs in about ~10ms for 100_000 messages
with_actor
runs in about ~4000ms for 100_000 messages
Is this caused by some context switching what is happening internally, or related how locking is done internally?Pablo
09/27/2021, 2:02 PMlaunch{}
?
Example :
private val job: Job? = null
internal class Foo : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + Job!!
...
private fun bar() {
job = Job()
launch { .... }
}
override onDetach() {
job.cancel()
}
}
Does it really cancel the launch?ursus
09/28/2021, 2:07 AMclass Foo {
private val scope = CoroutineScope(SupervisedJob() + Dispatchers.Main.immediate)
fun init() {
println("a")
scope.launch {
println("b")
flowOf("hello")
.collect {
println("c")
}
}
println("d")
}
}
I'm pretty sure this emmited printed a, b, c, d
in the past when using immediate main dispatcher on android
but now it emits a, d, b, c
Is there a way to make this blocking as it was in rxjava?
I thought this exactly was the difference between Dispatchers.Main
and Dispatchers.Main.immediate
luke_c
09/28/2021, 3:30 PMbenkuly
09/29/2021, 6:12 AMflow
and stateIn
or does it has unpredictable side effects. It does not feel right to pass this
into stateIn
. Does doSomething()
ever return or will it suspend infinite, because the scope is used by stateIn
?
suspend fun doSomeThing() = coroutineScope {
val someState = someFlow.stateIn(this)
...
}
Besto
09/29/2021, 4:11 PMonStart()
method in my Fragment A. This flow is attached to the viewModelScope
.
My question is: when I open another fragment and return to my Fragment A onStart
is called again and the register for the flow is called as well, so the flow is called twice without cancel it as it's attached to the viewModelScope
and the onCleared
method was not called yet .
How do you handle this case to avoid keeping emitting data to the view when the view is not attached ?nilTheDev
10/01/2021, 9:15 AMlaunch
or async
from suspend
function directly. suspend
functions get called from a coroutine
. Why can't it use that context. If we create a new coroutineScope
inside a suspend
function then would the new coroutines
be attached to the parent that called the suspend
function? So that these get cancelled when the parent get cancelled.K Merle
10/01/2021, 12:43 PMbenkuly
10/01/2021, 5:25 PMMutableStateFlow
as a cache for a database (sqldelight). It means, that e. g. I have fun getKeysForUser(userId:String):MutableStateFlow<Keys?>
, which first looks in the cache (just a MutableMap) if it contains a matching MutableStateFlow and if not, creates one with an initial value from the database. It then also launches a coroutine to collect from the MutableStateFlow and write to the database on changes. If there are no subscribers anymore (except from the db-collect), I want to remove it from the cache. What is the best way to do it and does this whole idea work?AmrJyniat
10/02/2021, 9:42 AMcombine
function in variable x as following:
val x = combine(flow1, flow2) { (flow1, flow2) ->
listOf(flow1, flow2)
}
when I call collect on x flow the result will be empty because flow1 and flow2 not ready yet, so I want to trigger collect on each flow gets ready to get latest data always.AmrJyniat
10/02/2021, 9:42 AMcombine
function in variable x as following:
val x = combine(flow1, flow2) { (flow1, flow2) ->
listOf(flow1, flow2)
}
when I call collect on x flow the result will be empty because flow1 and flow2 not ready yet, so I want to trigger collect on each flow gets ready to get latest data always.Dominaezzz
10/02/2021, 9:54 AMAmrJyniat
10/02/2021, 10:02 AMDominaezzz
10/02/2021, 10:24 AMstojan
10/02/2021, 2:30 PMflow1.onStart { emit(defaultValue) }
And the same for flow2nilTheDev
10/02/2021, 6:55 PM