zak.taccardi
11/18/2019, 5:27 PMSrSouza
11/18/2019, 9:48 PMPaul Woitaschek
11/19/2019, 8:13 AMPatrick
11/19/2019, 1:52 PMjeff
11/19/2019, 6:34 PMflow {
emit(expensiveNetworkOperation1())
emit(expensiveNetworkOperation2())
emit(expensiveNetworkOperation3())
}
and I want to collect that flow repeatedly, without repeating the expensive operations.
Here's my solution. Feedback? Gotchas I'm missing?
/**
* Collects the upstream source a single time, and sends cached values to any future collectors
*/
fun <T> Flow<T>.replay(): Flow<T> {
val upstream = this
return object : AbstractFlow<T>() {
val isFinished = AtomicBoolean()
val isCollecting = AtomicBoolean()
val values = mutableListOf<T>()
override suspend fun collectSafely(collector: FlowCollector<T>) {
coroutineScope {
// Collect the upstream flow (but only once) and remember the values it emits
if (!isCollecting.getAndSet(true)) {
launch {
upstream.collect { values += it }
isFinished.set(true)
}
}
// Create a downstream flow that just emits cached values
val downstreamFlow = flow {
var i = 0
while (!isFinished.get()) {
while (i < values.size) {
emit(values[i])
i++
}
delay(1)
}
}
collector.emitAll(downstreamFlow)
}
}
}
}
Slackbot
11/19/2019, 11:11 PMhenrikhorbovyi
11/20/2019, 12:48 PMQUESTION
]
Hello mates, I always hear people saying that RxJava is really powerful and coroutines it's a lighter solution. And they say that the situation to use RxJava is when you should use all those Rx operations (e.g. transform, map, etc
).
Thinking about this I was wondering if it's possible to do the same only with Coroutines and pure Kotlin, and if not, what is it so powerful in Rx?bod
11/20/2019, 2:47 PMAnimesh Sahu
11/20/2019, 3:35 PMJobCancellationException
at once?vineethraj49
11/20/2019, 4:12 PMwithContext
? (I’m trying to time some functions)dave08
11/20/2019, 4:43 PMshare
operator on Flow
(at least in the meantime until it shows up in the library)? As in https://github.com/Kotlin/kotlinx.coroutines/issues/1261 ... broadcastIn
and asFlow
is probably hot... so a fast upstream might lead to lost emissions unless a buffer is properly configured...pardom
11/20/2019, 6:30 PMjimn
11/20/2019, 8:14 PMkevin.cianfarini
11/20/2019, 10:17 PMDispatcher
?Yauhen Pelkin
11/21/2019, 9:26 AMjeggy
11/21/2019, 5:57 PMrocketraman
11/21/2019, 11:12 PMkill -5
to get insight into the running coroutines, but always see the "do not use in production" warnings.ubu
11/22/2019, 6:55 AMBLOB
objects. I implemented a service that fetches these images by leveraging the power of Kotlin Coroutines
. This service is quite simple, it just exposes a suspend fun
. I used it as one-shot operation inside a ViewModel
, so the scoping here was easy. Now I need to use this image loader inside a RecycerView
. So the scoping there gets tricky: how do I manage a CoroutineScope
inside a RecyclerView
? As far as I understand, I cannot use Glide
, Picasso
or Coil
, since it is bound to uri/url image sources. So I need to implement it myself. Where do I start? Should I somehow use a LifecycleOwner
in order to get that scoping feature?
Any help will be very appreciated. 🏅Jason
11/22/2019, 9:21 AM<http://Dispatchers.IO|Dispatchers.IO>
for call api from network. But I am confused that where need we specified the dispatcher.
For example:
I have a UserViewModel class and FetchUserUseCase class.
Pattern 1
1. FetchUserUseCase.kt
suspend fun fetch(): UserResponse {
return runCatching {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
clientApi.get()
}
}
}
2. UserViewModel.kt
fun fetch() {
viewModelScope.launch {
fetchUserUseCase.fetch()
}
}
Pattern 2
1. FetchUserUseCase.kt
suspend fun fetch(): UserResponse {
return coroutineScope {
clientApi.get()
}
}
2. UserViewModel.kt
fun fetch() {
viewModelScope.launch {
runCatching {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
fetchUserUseCase.fetch()
}
}
}
}
Above 2 way of writing is the same ?
Where need we specified dispatcher ? ViewModel or UseCaselouiscad
11/22/2019, 9:50 AMAnimesh Sahu
11/22/2019, 11:18 AMorg.jetbrains.kotlinx:kotlinx-coroutines-io-jvm:0.1.16
?Animesh Sahu
11/22/2019, 12:19 PMLulu
11/23/2019, 3:33 AMMutex
uses atomics internally too and that made me more confused. I also don't fully understand how atomics work in the first place, like in what case would they lock the object? Would putting my map in an atomic guarantees the operations performed will be synchronized? etckoufa
11/23/2019, 1:25 PMcallbackFlow
again. How would you review following code:
fun TextView.onTextChange() = callbackFlow {
val listener = addTextChangedListener { text ->
if (text != null) {
offer(text.toString())
}
}
awaitClose { removeTextChangedListener(listener) }
}
sajadgarshasbi
11/23/2019, 9:55 PMCoroutineScope
inside a compound view? is safe that pass LifecycleScope
from Fragment
to view? or i have to handle that with lifecycle events?Dan Lowe
11/24/2019, 8:01 AMBroadcastChannel
but ignore any items that may already be in that channel at the time of subscription. Is this possible? (example in thread)Alexey V Golubev
11/24/2019, 9:31 AMReceiveChannel
and do something like channel.asFlow().count()
?asad.awadia
11/24/2019, 3:55 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
stop my threads from getting 'blocked'?asad.awadia
11/24/2019, 3:56 PMrrva
11/25/2019, 4:38 PMrrva
11/25/2019, 4:38 PMwithoutclass
11/25/2019, 4:50 PMrrva
11/25/2019, 4:55 PMwithoutclass
11/25/2019, 4:57 PMSteve
11/25/2019, 5:49 PMwithoutclass
11/25/2019, 6:57 PMDico
11/26/2019, 1:03 AMspand
11/26/2019, 5:55 AM