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

**URL:** <https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416>\
**Category:** Android\
**Created:** [September 12, 2018, 8:29pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416 "2018-09-12T20:29:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [September 12, 2018, 8:29pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/1 "2018-09-12T20:29:58Z")

</div>

I read the blog post by @elizarov on the shift to [Structured concurrency](https://medium.com/@elizarov/structured-concurrency-722d765aa952) 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… }](https://github.com/salamanders/deconstructed-camera/blob/master/mobile/app/src/main/java/info/benjaminhill/deconcamera/MainActivity.kt#L30)

… 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?](https://github.com/salamanders/deconstructed-camera/blob/master/mobile/app/src/main/java/info/benjaminhill/deconcamera/LazySuspend.kt)”

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

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [September 13, 2018, 9:41pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/2 "2018-09-13T21:41:06Z")

</div>

Check out the updated coroutine UI documentation on [“Structured concurrency, lifecycle and coroutine parent-child hierarchy”](https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#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.

---

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [September 14, 2018, 5:36pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/3 "2018-09-14T17:36:23Z")

</div>

Oh! huh!  
Well, I [made my own ScopedAppActivity copying the example](https://github.com/salamanders/deconstructed-camera/blob/master/mobile/app/src/main/java/info/benjaminhill/deconcamera/EZPermissionActivity.kt#L15), and it seems to work… so… yay?

---

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [September 16, 2018, 4:13pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/4 "2018-09-16T16:13:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [September 21, 2018, 6:45pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/5 "2018-09-21T18:45:53Z")

</div>

I ran into an oddity:  
`val deferred = (1..maxAttempts).map { attemptNum -> async { ...do stuff`  
stopped using up all cores, seemed to be constrained to 100% cpu. (which is only 1 of my 8 cores)

Adding in `async(Dispatchers.Default) {` to the async call started running everything in parallel again, correctly using 700% cpu (7 of 8 cores)

---

<div class="post-metadata">

**Author:** ![draskosaric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/draskosaric/32/5663_2.png) [@draskosaric](https://discuss.kotlinlang.org/u/draskosaric)\
**Post date:** [October 30, 2018, 6:52am UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/6 "2018-10-30T06:52:51Z")

</div>

So, did you solve the problem?

---

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [November 1, 2018, 10:56pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/7 "2018-11-01T22:56:50Z")

</div>

Yes, but it felt funny implementing “CoroutineScope” and manually putting in the lines below. I was hoping for more “it just works!” parallel magic. But once I had this, all the sub-classes could use `launch {...` without issue.

```
abstract class ScopedActivity : AppCompatActivity(), CoroutineScope {
    private lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.Main

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        job = Job()
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }
}

```

---

<div class="post-metadata">

**Author:** ![benjaminhill](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@benjaminhill](https://discuss.kotlinlang.org/u/benjaminhill)\
**Post date:** [November 6, 2018, 5:57pm UTC](https://discuss.kotlinlang.org/t/moving-to-1-3-coroutines-dont-quite-get-it-but-tried-anyway-correctly/9416/8 "2018-11-06T17:57:14Z")

</div>

And in [another project,](https://discuss.kotlinlang.org/t/actor-examples-and-sequences-which-is-1-3-correct/10144/9) I did **not** get it to work, and instead ripped out everything Kotlin Sequence/Actor/Coroutine/Channel and went back to plain java threads + Blocking Queue. Which makes me sad because I feel like it would be easier with Kotlin if I knew how to make it work.
