https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

Davide Giuseppe Farella

03/01/2019, 1:08 PM
TLDR; this is what worries me more, is there anything wrong here?
Copy code
override suspend fun observeCount() = coroutineScope {
    queries.count().asChannel().mapToOne( coroutineContext ).map { it.toInt() }
}
l

louiscad

03/01/2019, 2:35 PM
What is
mapToOne
?
d

Davide Giuseppe Farella

03/01/2019, 3:17 PM
Copy code
fun <T : Any> ReceiveChannel<Query<T>>.mapToOne( context: CoroutineContext ) =
    map( context ) { it.executeAsOne() }
l

louiscad

03/01/2019, 5:22 PM
What are you expecting exactly in this snippet, and what happens exactly at runtime instead?
d

Davide Giuseppe Farella

03/01/2019, 11:53 PM
I expect the ViewModel to receive the UiModel's from the Presenter, but it doesn't 😅
I see that the Channel created from the listener
offers
the Queries, but presenter never receive them
@louiscad you should take a look at the full snippet above for get a better idea 🙂 it's 90 lines of code but half are empty and very easy to understand 😋
l

louiscad

03/02/2019, 8:19 AM
There's a problem in invokeOnClose, you don't seem to pass a proper lambda. Also, the indentation doesn't follow the convention, I'm not sure why you didn't reformat at least to post here
The observe function of your presenter can't return until the channels are closed because the coroutines launched in the scope are not finished. You should not have it a suspending function but have it a regular function, extension for
CoroutineScope
instead.
d

Davide Giuseppe Farella

03/02/2019, 8:28 AM
I'm not getting it 🤔 the function on presenter correctly reaches its end 🤔 I will give a try, but I didn't really get it 😁 Why you saying the lambda on
invokeOnClose
is not right? The indentation stuff it's about AS beta that indentates wrongly, don't ask me why
It works 🙄
l

louiscad

03/02/2019, 9:01 AM
You can edit Kotlin code style, loading it from convention.
You mean you no longer have a problem with this code?
d

Davide Giuseppe Farella

03/02/2019, 9:03 AM
Yep, I did some edit for get rid of the other
coroutineScope {  }
In the Source
But I didn't get why it's working now ☹️ could you explain or give me so useful link?
l

louiscad

03/02/2019, 9:38 AM
As stated in the docs, the
coroutineScope { … }
function suspends until all coroutines launched inside it are completed or cancelled.
d

Davide Giuseppe Farella

03/02/2019, 9:40 AM
Now it's more clear, thanks 🍻
3 Views