Ayfri
07/16/2022, 4:18 PMStephen Edwards
07/18/2022, 5:54 PMkotlinx.coroutines.test
migration guide for 1.6+ when talking about replacing runBlockingTest
with runTest
there is a little line:
It works properly with other dispatchers and asynchronous completions.
No action on your part is required, other than replacing runBlocking with runTest as well.
What is meant by this? Why does runBlocking
need to be replaced with runTest
if there is no dispatcher/virtual time manipulation?myanmarking
07/19/2022, 3:20 PMKulwinder Singh
07/20/2022, 6:20 AMclass MyObject private constructor() {
companion object {
@Volatile
private var INSTANCE: MyObject? = null
fun isReadyInstance(): Boolean {
return (INSTANCE != null)
}
fun getInstance(): MyObject {
return INSTANCE ?: throw Exception("Error Instance is not initialized.")
}
fun createInstance() {
synchronized(this) {
if (INSTANCE == null) {
INSTANCE = MyObject()
}
}
}
fun deleteInstance() {
synchronized(this) {
INSTANCE = null
}
}
}
there is condition in my code to check for instance
if (MyObject.isReadyInstance()) {
MyObject.getInstance() //BUT HERE I GET -> "Error Instance is not initialized."
}
I think when isReadyInstance
returned true meanwhile at some other place deleteInstance
is called and therefore getInstance
returned null, but how can i make sure that it won’t happen ?Matteo Mirk
07/20/2022, 12:51 PMclass PublishingServerCall<ReqT, RespT>(
next: ServerCall<ReqT, RespT>,
val publisher: EventPublisher
) : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(next) {
override fun close(status: Status, trailers: Metadata) {
if (!status.isOk) {
runBlocking {
supervisorScope {
launch(Dispatchers.Unconfined) {
publisher.publishError(status)
}
}
}
}
super.close(status, trailers)
}
}
I know there’s lot of grpc-specific stuff but bear with me, because it’s important details. Here is an implementation of a io.grpc.ServerCall
, an abstraction needed by gRPC infrastructure to implement a custom interceptor. As you see we override the close()
function because we’re interested in firing an event when the call is returning from a service and we have a response Status
.
We thought that this setup would allow us to publish the event asynchronously and move on in the interceptors chain without waiting for the publisher to complete. It seems to be working so far, but we only did some manual tests on our machines, it hasn’t been deployed yet. But the more I look at this code, the more I’m convinced it’s not doing what we thought: I have a feeling that the whole runBlocking {}
part is useless and it’s blocking the thread anyway waiting for the return, and it would be the same to just invoke the publisher directly. Of course it’s needed because the enclosing function is not suspendable, but I’m full of doubts and don’t know if we wrote the right thing.
Is this function blocking the request thread or is it returning right away, without waiting for the publication?
Although skilled in service programming and Kotlin, we’re pretty much beginners on coroutines, so please if anyone has the expertise an advice will be much appreciated, thank you!Dean Djermanović
07/21/2022, 1:22 PMval source = flow {
emit(1)
delay(200)
emit(2)
delay(200)
emit(3)
}
source
.onEach { delay(500) }
.collect {
...
}
I want my items to be collected after:
1 - 500ms
2 - 700ms
3 - 900ms
Slackbot
07/21/2022, 1:28 PMGeorge
07/22/2022, 1:15 PMMark
07/22/2022, 2:54 PMFlow<T>.distinctUntilChanged(areEquivalent: (old: T, new: T) -> Boolean)
but emits the newest of the equivalent values instead of the oldest?Lilly
07/23/2022, 6:30 PMflow of T
if cache is empty, otherwise T (as flow). I'm wondering which way is more appropriate:
fun fetchStuff(): Flow<Stuff> =
if (cache.isNotEmpty()) {
cache.asFlow()
} else {
api.fetchStuff()
}.map { model -> model.toStuffModel() }
or
fun fetchStuff(): Flow<Stuff> = flow {
if (cache.isNotEmpty()) {
emit(cache.first())
} else {
emitAll(api.fetchStuff())
}
}.map { model -> model.toStuffModel() }
Slackbot
07/24/2022, 12:44 AMursus
07/24/2022, 4:43 PMJob
. What is this job? Is it already cancelled or what is it?
Wouldn't be cleaner to throw?Colton Idle
07/25/2022, 6:45 AMLukas Lechner
07/25/2022, 3:12 PMtseisel
07/26/2022, 6:52 PMval source = flow {
emit("A")
delay(100)
emit("B")
delay(50)
emit("C")
emit("D")
}
when applied a constraint of "max 1 element each 100ms", it should emit:
• "A" at t = 0ms
• "B" at t = 100ms
• "D" at t = 200msfitermay
07/27/2022, 8:30 PMfitermay
07/27/2022, 8:32 PMLukas Lechner
07/28/2022, 10:10 AMSharedFlow
and performs network request every minute while there are active subscribers and emits the result. I don't want to perform network requests when the app is in the background.
Should it be the responsibility of the datasource to stop performing network requests when the app goes into background, or should it be the responsibility of the clients of the data source to cancel their coroutines that collect from the datasource?
If it's the responsibility of the clients, then one faulty client implementation can make the datasource to stay active ....Loney Chou
07/28/2022, 3:31 PMresumes
mean for continuations? Where and when the code after suspension point run? Like if the continuation runs in an infinite loop, does it mean that resumes
will never end?Lucas
08/02/2022, 1:27 AMdeviant
07/30/2022, 11:04 AMselect
builder? something like this
flow {
emit(select {
someFlow1.onCollect()
someFlow2.onCollect()
})
}
dimsuz
08/02/2022, 1:29 PMCoroutineExceptionHandler
between several `CoroutineScope`s?Jesse Hill
08/02/2022, 7:09 PMColton Idle
08/03/2022, 3:37 PMfun getBooks(): Flow<List<Book>> {
return callbackFlow {
val listener =
FirebaseFirestore.getInstance()
.collection("books")
.addSnapshotListener { value, error ->
if (error != null || value == null) { /*dosomething*/ }
var books: List<Book>? = null
runBlocking(<http://Dispatchers.IO|Dispatchers.IO>) {
books = (value!!.toObjects())
}
trySend(books!!)
}
awaitClose { listener.remove() }
}
}
this seems to work well... but to stress test it I wrapped books = (value!!.toObjects())
with repeat (1000)
and now my UI hangs. Shouldn't the dispatchers.io take care of this?
runBlocking(<http://Dispatchers.IO|Dispatchers.IO>) {
repeat(1000) {
books = (value!!.toObjects())
}
}
Jan
08/04/2022, 5:32 PMFudge
08/05/2022, 6:55 AMJobCancellationException
. How can I pinpoint the exact line of code that actually caused the coroutine to cancel? I've tried setting an exception breakpoint for JobCancellationException
, but it doesn't get triggered for some reason.Colton Idle
08/07/2022, 1:21 AMwithContext(Dispatchers.Default){
but now I get a ConcurrentModificationException
. I could understand if I was adding/removing to the list on Dispatchers.Default while main thread was swapping the list entirely... but I'm just searching via indexOfFirst
. thoughts?Colton Idle
08/07/2022, 5:14 PMfetchMoreData()
fun fetchMoreData(i) {
viewModelScope.launch { ...
but now if I call fetchMoreData() again, I actually want to cancel the old data fetching. How would I do that?Juan Rada
08/08/2022, 2:40 PMprivate suspend fun process() = coroutineScope {
getFlow()
.map { async(<http://Dispatchers.IO|Dispatchers.IO>) { ioBlockingFunction(it) } }
.buffer(20)
.map { it.await() }
.collect()
}
my expectations is that 20 blocking operations will be trigger at time, then they will be awaited and then next 20 blocking operations will be trigger. What I am noticing is that somehow flow stop producing values and my app got block at flow element 25 of 30, or 100 (at a random point), even when flow function has more records to produceRobert Williams
08/08/2022, 4:27 PMCoroutineDispatcher.dispatch
method can be called directly in application code and this seems… problematicRobert Williams
08/08/2022, 4:27 PMCoroutineDispatcher.dispatch
method can be called directly in application code and this seems… problematicdispatcher.dispatch(dispatcher) {
//some blocking code
}
ephemient
08/08/2022, 4:52 PMdispatcher.asExecutor().execute {
//some blocking code
}
Zach Klippenstein (he/him) [MOD]
08/08/2022, 4:54 PMRobert Williams
08/08/2022, 5:20 PMZach Klippenstein (he/him) [MOD]
08/08/2022, 5:26 PMRobert Williams
08/08/2022, 5:32 PMephemient
08/08/2022, 5:35 PMAdam Powell
08/08/2022, 7:18 PMCoroutineContext
in more APIs