jeggy
06/11/2020, 9:29 AMrunBlockingTest
and am now getting: java.lang.IllegalStateException: This job has not completed yet
Is there some nice way to figure out which job has not been completed? Or do I need to find that out myself?vaskir
06/11/2020, 1:38 PMactor { }
ubu
06/11/2020, 4:23 PMvalue
in StateFlow<Map<Id, T>>
, so that current subscriber does not a receive this update? I have a particular use case from UI programming.
Suppose that this T
is String
. We have a list of texts, that is rendered first in a text editor. This text is then edited by our user and we receive onTextChanged(id, text)
event. We now need to sync this text change with our state: map[id] = text
, but we do not need to render this state again, because UI already has it.
Or maybe there is no way to resolve this problem with StateFlow
?Pacane
06/11/2020, 4:45 PMsupervisorScope
and pass it a parent supervisorJob? From the docs it says I can, but I don't see how I can pass it downRechee Jozil
06/11/2020, 6:17 PMgotoOla
06/11/2020, 8:15 PMLuis Munoz
06/12/2020, 4:14 PMYevhenii Nadtochii
06/13/2020, 3:22 AMfun simpleChannelTest() {
val channel = Channel<String>(capacity = Channel.Factory.UNLIMITED)
val backgroundProcessor = MainScope().launch {
for (str in channel) {
println("$str has been processed")
}
}
runBlocking(Dispatchers.Default) {
val producers = launch {
repeat(100) {
launch {
channel.send("str$it")
}
}
}
println("producers have been created")
producers.join()
println("producers have done their work")
channel.close()
println("channel has been closed")
backgroundProcessor.join() // ********************* I"M STUCK HERE
println("backgroundProcessor coroutine has done its job") // ***************** UNREACHABLE
}
Alexey Demedeckiy
06/13/2020, 7:08 PMclass Store {
private val channel = Channel<Action>()
suspend func dispatch(action: Action) {
channel.send(action)
}
init {
GlobalScope.launch {
for (action in channel) {
reduce(action)
notifyObservers()
}
}
}
}
Sadly, this code doesn't work - actions are not reducer.
Small detail: If i will explicitly set Dispatchers.Main in init and in dispatch - it work, but defeats my purpose.
My goal is to move action processing of main thread. notifyObservers
will build Props
for each active ViewModel
and publish.nicholasnet
06/14/2020, 5:15 AMSaurabh
06/14/2020, 6:10 AMfitken
06/14/2020, 11:14 AMoverride suspend fun getMovieDetails(movieId: Int): LiveData<MovieEntity> {
GlobalScope.launch {
async {
try {
val movieData = mService.getMovieDetails(movieId)
mCache.updateMovie(movieData)
Rose.error("done") // *(2)*
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Rose.error("run first") // *(1)*
return Transformations.map(mCache.getMovie(movieId)) {
return@map MovieEntity(....)
}
}
I want (1) run first, then run (2).
I managed to do that with GlobalScope, but GlobalScope is highly discouraged (I read this here https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/index.html)
I also searched on the internet and in this channel with keyword: async suspend function. But haven’t found any answer.
Is there any other way to do that?
Could anyone help me on this? Thank you so much.Alex Gotev
06/14/2020, 4:44 PMjeggy
06/14/2020, 5:23 PMyield()
every now and then all around in the code, so if any cancelation is requested that all my coroutines will cancel? And if so, how expensive is it to call yield
?dimsuz
06/14/2020, 8:29 PMlaunch {
try {
myCoroutine() // throws exception when executed
} catch (e: Exception) {
println("caught $e")
}
}
and instead of catching it crashes. But if I replace try/catch with
runCatching { myCoroutine() }.exceptionOrNull()?.let { println("caught $e") }
it catches and prints exception.
Now, myCoroutine
above involves calling android-related Retrofit library which has it's own coroutine adapters, so it might be doing some smarty stuff (debugger shows that continuation.resumeWithException()
is called, maybe on different dispatcher), but I want to understand in general the difference between try/catch
and `runCatching`: why can this situation happen and when I should use which approach to handling exceptions?Saurabh
06/15/2020, 8:47 AMcombineLatest
operator is deprecated for flows. Is there a way to achieve similar results with some other api in flow?Gabriel Feo
06/15/2020, 5:09 PMKoya Sivaji
06/15/2020, 7:47 PMI am new to kotlin flows and trying to add Flows to one of my project and facing an issue while converting list of objects of one type to another as shown below. Basically, am not sure which operator to be used for this.
//Repository - method return type is Flow<List<HistoryEvent>>
fun getHistoryEvents(): Flow<List<HistoryEvent>>
//ViewModel - calling repository method and trying to convert data to List<AccessoryEvent>
val accessoryEvents : LiveData<List<AccessoryEvent>>= liveData{
repository.getHistoryEvents()
// what operator to be used to convert from List<HistoryEvent> to List<AccessoryEvent>
.collect()
}
Brendan Weinstein
06/16/2020, 1:23 AMnative-mt
into master with kotlin 1.4? If not, what's the pulse check (3 vs 6 months away)?
I am preparing an internal doc for evaluating kotlin multiplatform and a few coworkers have already asked this. Having stable multi-threaded coroutines for kotlin-native changes how folks perceive kmp viability.samuel
06/16/2020, 5:09 PMwithTimeout(20000) {
try{
while (true) {
// read input from socket and exit while block when exception is thrown
// or expected data has been received
}
}catch(e: Exception) {
// Handle the exception here
}
}
I also tried to use this, but the code within`scope.launch` was never executed but the timeout worked as expected
val scope: CoroutineScope = CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO>)
withTimeout(20000){
suspendCancellableCoroutine<String>{ continuation ->
continuation.invokeOnCancellation {
// Handle my exception here
}
scope.launch {
while (true) {
// read input from socket and exit while block when exception is thrown
// or expected data has been received
}
}
}
}
Could someone please help give me some pointers in what i might be missing or even perhaps a better way to approach this?Luis Munoz
06/16/2020, 5:14 PMRechee Jozil
06/17/2020, 6:27 AMCoroutineLiveData
is hidden...louiscad
06/17/2020, 8:16 AMContinuationImpl
classes whenever possible. What do you think?
Details here: https://youtrack.jetbrains.com/issue/KT-39644jeggy
06/17/2020, 10:29 AMfun main() = runBlocking {
repeat(50) {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
delay(5000L * it)
println("Done $it")
}
}
}
Having some code like this. Is it possible to leak coroutines, if I were to just send the kill signal to this process?George Theocharis
06/17/2020, 4:00 PMevery { presenter.scope } returns TestCoroutineScope()
and I was trying to verify in order some calls including the invokeAsync I mentioned.
The exception I am getting is an abstract method error on newCoroutineContext that i cannot think of a solution.
The code works, the test not. If you also debug it also works correctly until its time to return to the java side.
Sorry for the wall its a bit complicated and haven’t seen it previously.
Ps i am using dispatchers.setMain and the coroutines test dependency.itnoles
06/17/2020, 7:27 PMLuoqiaoyou
06/18/2020, 10:26 AMUnresolved reference: newSingleThreadContext
in iosMain sourceSet. Was it removed in this version?Slackbot
06/18/2020, 2:41 PMBrandon Trautmann
06/18/2020, 4:58 PMviewModelScope.launch {
customerRepo.customer(customerUid) // Returns a `Flow<Customer>`
.combine(controllerRepo.controllersForCustomer(customerUid)) { customer, controllers ->
CustomerWithControllers(customer, controllers)
}
.collect { customerWithControllers ->
internalState.value = State.Loaded(customerWithControllers)
}
}
This works just fine, but when I go to delete a customer
, the deletion cascades to the controllers
and what you end up with is a new CustomerWithControllers
with empty controllers because the customer
Flow
last emission was the customer that was just deleted. I solved this by holding onto the Job
returned by launch
, and when I go delete a customer, I basically cancel that Job
, delete the customer, and then execute a function that checks for the existence of any customer and if one doesn't exist, brings the user to a FTUX screen. Is this a decent way of handling this? It'd be nice if Room
had some way to emit an Empty
token or something to indicate the table is empty 🤔katokay
06/21/2020, 12:09 PMkatokay
06/21/2020, 12:09 PM