Marko Mitic
11/04/2019, 1:44 PMrunCatching
that doesn't catch CancellationException
?
Would be nice to have some standardized solution (and IDE warnings on catching the CancellationException
in coroutines)Vincent Williams
11/04/2019, 5:13 PMLuis Munoz
11/04/2019, 8:04 PMJohn
11/04/2019, 9:48 PMvineethraj49
11/05/2019, 12:57 AMtest1()
and test2()
end up doing the same? (my guess: yes)
test1: async(<http://Dispatchers.IO|Dispatchers.IO>) { [...] }
test2: suspend fun logic() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { [...] }; async { logic() }
if so, which is more idiomatic?vineethraj49
11/05/2019, 1:05 AMcoroutineContext
like coroutineContext[Job]
that propagates to all children;
if yes, are there any pointers for doing the same?Kroppeb
11/05/2019, 3:09 AMrunBlocking {
fun tt(s: CoroutineScope) = s.launch(start = CoroutineStart.LAZY) { }
val s = this
tt(s)
}
Slackbot
11/05/2019, 8:13 AMFrank Feng
11/05/2019, 9:22 AMThere is no event loop. Use runBlocking{...} to start one
I run the kotlin native lib on iOS, it throw this error.Klara Erlebachova
11/05/2019, 5:09 PMrunBlocking
with K/N? I used it in my MPP project for synchronous API and works fine on Android but on iOS it never returns. Even very simple example like this:
val result = runBlocking(MainDispatcher) {
return@runBlocking "yes"
}
kd
11/05/2019, 7:00 PMubu
11/05/2019, 8:59 PMViewModel
observing flow of user accounts: Account1
, Account2
, `Account3`… Then for each of these accounts I need to load an image (represented as blob
) from some remote source. This loading may take a while, that’s why I show user accounts first without their picturse, then update UI
with the same model having now picture data. First of all, it feels a bit weird to load images inside onEach
operator. Or not? And then, maybe it’s not a good idea at all to load picture data inside a ViewModel
. What do you think?
class SelectAccountViewModel(
private val startLoadingAccounts: StartLoadingAccounts,
private val observeAccounts: ObserveAccounts,
private val loadImage: LoadImage
) : ViewModel(), SupportNavigation<Event<AppNavigation.Command>> {
val state by lazy { MutableLiveData<List<ChooseProfileView>>() }
private val accountChannel = Channel<Account>()
private val imageChannel = Channel<ImageBlob>()
init {
startObservingAccounts()
//startLoadingAccount()
viewModelScope.launch {
val accumulatedAccounts = accountChannel
.consumeAsFlow()
.onEach { account -> loadImage(account) }
.scan(emptyList<Account>()) { list, value -> list + value }
val accumulatedImages = imageChannel
.consumeAsFlow()
.scan(emptyList<ImageBlob>()) { list, value -> list + value }
accumulatedAccounts.combine(accumulatedImages) { accounts, images ->
accounts.associateWith { account ->
images.firstOrNull { it.id == account.id }
}
}.collect { result ->
state.postValue(
result.map { (account, image) ->
ChooseProfileView.ProfileView(
id = account.id,
name = account.name,
image = image?.blob
)
}
)
}
}
}
}
And then, these other questions arise: is it okay to use channels as shown above? Could one be more concise with coroutines?zak.taccardi
11/05/2019, 10:10 PM1.4.0-RC
?vineethraj49
11/06/2019, 8:42 AMasync(Dispatchers.Default)
on runBlocking
) ok, or will this blow up in my face?Derek Berner
11/06/2019, 8:25 PMJason
11/07/2019, 3:32 AMflow
. I just want to looping forever by using Flow of coroutine. Actually I dont like while(true). Any support method ?dariuszbacinski
11/07/2019, 8:36 AM@Test
fun coroutineTest() {
runBlocking {
flowOf("any").map {
val result = ioFunction() //crash java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to java.util.Map
result
}.collect { result ->
Log.v("$result")
}
}
}
suspend fun ioFunction(): Map<Long, Int> = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
mapOf(1L to 4)
}
how can is use suspend function in when mapping Flow? above code crashes, but it works when I remove suspend function and use normal blocking functionPaul Woitaschek
11/07/2019, 9:20 AMDispatchers.Main.immediate
that causes this behavior?ghedeon
11/07/2019, 1:11 PMDispatchers.Main.immediate
, what is the the best way to execute a portion immediately and launchAndForget
the rest?
Ex:
suspended fun foo(){
operationA() // <--- don't wait for it, it's on IO
operationB() // <--- post on Main immediately
}
It already works if I swap the order, like
operationB() // <--- post on Main immediately
operationA() // <--- don't wait for it, it's on IO
But I'm looking for a way that doesn't depend on the order of operations.elaborate tissue
11/07/2019, 2:28 PMrunBlocking {
launch (Dispatchers.Unconfined) {
// check every x seconds if file exists, if it does then kill app (and all other coroutines)
}
launch (<http://Dispatchers.IO|Dispatchers.IO>) {
// every x seconds send metrics to server
}
for (i in 1..Runtime.getRuntime().availableProcessors()) {
launch (Dispatchers.Default) {
// use remaining cpu/mem resources to process batch items
}
}
}
running this means that when all available processors are doing work, the jobs which are supposed to run every x seconds never run, how am I supposed to get them to run regularly regardless of if available processors are performing the batch work?James
11/07/2019, 5:17 PMjeff
11/07/2019, 6:17 PMmyFlow
.flatMapMerge { item ->
flow { emit(suspendingFunctionThatMakesNetworkCall(item)) }
}
HOWEVER, Flow's flatMapMerge() has the following comment
Note that even though this operator looks very familiar, we discourage its usage in a regular application-specific flows.
Most likely, suspending operation in [map] operator will be sufficient and linear transformations are much easier to reason about.
I can't figure out how to do this with just a suspend map. Help?David Glasser
11/07/2019, 7:59 PMfun main() = runBlocking {
We just discovered that this leads to coroutines being run single-threaded by default, not in Dispatchers.Default. This is a big surprise! Is the normal pattern supposed to be fun main() = runBlocking(Dispatchers.Default) {
?Vsevolod Ganin
11/07/2019, 8:17 PMGlobalScope.launch(Dispatchers.Unconfined)
will actually dispatch? I stumbled upon some case when GlobalScope.launch(start = CoroutineStart.UNDISPATCHED, context = Dispatchers.Unconfined)
!= GlobalScope.launch(Dispatchers.Unconfined)
but I can’t quite figure what’s causing itSlackbot
11/07/2019, 9:23 PMJérôme Gully
11/07/2019, 10:45 PMGlobalScope.launch { }
to run an infinite while to draw on canvas (Android SurfaceView) or it is a better solution ?Kulwinder Singh
11/08/2019, 8:12 AMLiveData<List<User>>
loaded from firestore now i want to load `Appointment`s for each user from firestore, in end i want to create something like this Map<User,Appointment>
.
which flow
operator can helps me in this situation, or flow is not best suited for this?tseisel
11/08/2019, 9:36 AMFlow
?
1. Map T to a Deferred<R>
, then await each ?
2. Use plain coroutines with channels ?vineethraj49
11/08/2019, 10:13 AMlaunch(customDispatcher)
block if customDispatcher can’t find a free thread?Jakub Walusiak
11/08/2019, 1:21 PMJakub Walusiak
11/08/2019, 1:21 PMKroppeb
11/08/2019, 1:46 PMCompletableDeferred
instead of a channel.Jakub Walusiak
11/08/2019, 2:04 PMKroppeb
11/08/2019, 2:47 PMJakub Walusiak
11/08/2019, 2:47 PMKroppeb
11/08/2019, 2:50 PMJakub Walusiak
11/08/2019, 2:52 PM