https://kotlinlang.org logo
Title
p

Pitel

05/16/2022, 10:43 AM
I have code that basicaly looks like this:
someFlow.collect {
  Log.d(TAG, ">")
  // Some stuff
  Log.d(TAG, "<")
}
So, I should in Android's logcat alternating arrows going left and right. And in most cases, I do. But on some rare occasions, I see 2 going right and 2 going left. Any idea why that might happen?!
m

myanmarking

05/16/2022, 10:49 AM
two possible answers that i can think of. First, ‘some stuff’ is cancellable, and you have some ‘latest’ operator above in the chain. Or second, the logcat is not reliable and messes the order sometimes.
p

Pitel

05/16/2022, 10:53 AM
Hmm, ther is actualy
conflate
above the
collect
. But, it shouldn't cause the cancelation, no?
And even if id did, Then should just see the first arrow, and not the last, right? But I always see the correct number of pairs, but just the ordering is wrong.
m

myanmarking

05/16/2022, 10:57 AM
yes, it may be possible logcagt messes up the order
just add the timestamp before the arrow
then confirm even out of order the stamp is ordered
p

Pitel

05/16/2022, 10:58 AM
Good idea with those timestamps! Gonna try it. But I'm afraid logging is correc, because when this "glitch" happens, I see actual error in displayed data in my app.
Yup, the manual timestamps in logs are oredered correctly.
b

bezrukov

05/16/2022, 11:34 AM
try to verify these logs come from the same coroutine:
someFlow.collect {
  val identity = Random.nextInt()
  Log.d(TAG, "> $identity")
  // Some stuff
  Log.d(TAG, "< $identity")
}
1
y

yschimke

05/16/2022, 11:35 AM
Maybe helpful to have a second one outside the collect call, to show they are from the same flow you are collecting from.
b

bezrukov

05/16/2022, 11:37 AM
yes, as Yuri said, this one will show you the full picture:
val identity = Random.nextInt()
someFlow.collectIndexed { index, value ->
  Log.d(TAG, "> $identity => $index")
  // Some stuff
  Log.d(TAG, "< $identity => $index")
}
👍🏻 1