<Six simple ways to start a coroutine>—because fin...
# feed
s
Six simple ways to start a coroutine—because finding a way to actually call a suspending function in the first place can feel like one of the biggest roadblocks to getting started with async Kotlin.
It's great to have time to write some other stuff again now that my book is finally close to being finished! This article is a bit different from what I've written before—designed to be more succinct and actionable for new coroutine users, rather than an in-depth exploration. I hope it's useful!
c
Any reason to recommend
MainScope
over a custom
CoroutineScope
with your own
Job
and a dispatcher?
MainScope
is single-threaded, isn't it?
a
Yeah, that will be a must-buy for me!
s
Any reason to recommend
MainScope
over a custom
CoroutineScope
[…]?
Just that it's easier. When MainScope is available, it "just works"—you don't need to know what a
Job
is, and you avoid threading issues in UI code. I guess I'm treating multithreading and custom jobs as more advanced features that you can add in later, if you need them.
c
I see. I think it's a missed opportunity that the only multithreaded approach in the article is
GlobalScope
. I think people who want to learn coroutines tend to be interested in multithreading.
s
That's good feedback, thanks! I'll have a think about how I could incorporate it into the article.
👍 1
c
Great article though! Really well written. But I think it needs a mention of multithreading to become the true reference on the topic
p
I would emphasize more in not using runBlocking at all. I had it in my App in production for one release and I had to remove it. It crashed with really cryptic messages
s
Thanks for the comments! I've made a few changes.
k
That's a very good article, Sam. However, I'm still unsure what the best option is for a typical situation of a Spring Boot application, where a non-suspend callback function (called from a Spring component) has to call a suspend function. In fact, what I would find a great reference tool would be a flowchart-like decision tree, with questions like "Is your app an Android app?" - then do ABCD; else - is your app a desktop application? then next question, do you have a function that can't be a suspend function (because it's called from a Java method) but has to call a suspend function? then do XYZ.
s
This is a great idea, maybe I'll try to make something like that! Need to find a good tool for making an interactive flowchart like that...