Moving to 1.3 coroutines - don't quite get it, but tried anyway! Correctly?


#1

I read the blog post by @elizarov on the shift to Structured concurrency and it was a bit… woosh. (which is fine, I’m not the target audience)

I never fully understood why launch(UI) { ... } was good, bad, or even necessary, and to be honest, I don’t want to know - I want things to work without crashing, it is ok if I don’t get how the underlying system handles it.

So I went ahead and accepted the IDE’s suggestions, and ended up with calls like GlobalScope.launch(Dispatchers.Main) { …do stuff… }

… is this good? Do I like GlobalScope? Do I like Dispatchers.Main? (and do they like me back enough not to throw random errors like when I used launch(UI) incorrectly?) Because it is in an Android activity, should I even be using any of that and would a launch { ... } be better?

And if you know the answer to those, I’m betting you know the answer to “is this a sane way to do lazy suspend?

Yours in accepting “accept suggestion” when the IDE says to
-Benjamin


#2

Check out the updated coroutine UI documentation on “Structured concurrency, lifecycle and coroutine parent-child hierarchy”. Your Activity should implement the new CoroutineScope interface. Then you can launch a coroutine on the UI thread with launch { }, and it will automatically be cancelled when the Activity is destroyed.


#3

Oh! huh!
Well, I made my own ScopedAppActivity copying the example, and it seems to work… so… yay?


#4

And today with new releases, it now says I’m doing it all wrong. :confused: I think I should stick with 1.2 until things settle.