<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce" -->

---
title: From viewModelScope to Custom Classes: Safe Coroutine...
description: A step-by-step guide to building lifecycle-aware, self-cancelling coroutine owners in Kotlin without relying on Android&#x27;s viewModelScope. Starting from a...
canonical: https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: From viewModelScope to Custom Classes: Safe Coroutine Architecture in Kotlin | daily.dev
og:description: A step-by-step guide to building lifecycle-aware, self-cancelling coroutine owners in Kotlin without relying on Android&#x27;s viewModelScope. Starting from a...
og:url: https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce
og:image: https://api.daily.dev/og/posts/HRLKDOrcE.png
og:image:alt: From viewModelScope to Custom Classes: Safe Coroutine Architecture in Kotlin
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# From viewModelScope to Custom Classes: Safe Coroutine Architecture in Kotlin

**[ProAndroidDev](https://daily.dev/sources/pand)** · 6 min read · 0 upvotes · 0 comments

## Summary

A step-by-step guide to building lifecycle-aware, self-cancelling coroutine owners in Kotlin without relying on Android's viewModelScope. Starting from a simple class, the post walks through the pitfalls of anonymous CoroutineScopes and manual cancellation, then introduces a proper architecture using a separate CoroutineRunner class that holds a WeakReference to its owner. When the owner is garbage collected, the scope cancels automatically. The final design hides the CoroutineRunner behind an interface, exposes only launch() and close(), and supports both automatic GC-triggered and manual cancellation while preventing runaway coroutines.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://proandroiddev.com/stop-leaking-coroutines-the-right-way-to-make-classes-coroutine-scoped-a45df57cbef0>

## Similar posts on daily.dev

- [ViewModelScope Internals: A Deep Dive into Android’s Threading Magic](https://daily.dev/posts/viewmodelscope-internals-a-deep-dive-into-android-s-threading-magic-bbobu0rok) · droidcon · 0 upvotes · 0 comments
- [Coroutines, the Right Way: Where Your Money Code Actually Runs](https://daily.dev/posts/coroutines-the-right-way-where-your-money-code-actually-runs-hckmygkn4) · Medium · 0 upvotes · 0 comments
- [ViewModelScope Internals: A Deep Dive into Android’s Threading Magic](https://daily.dev/posts/viewmodelscope-internals-a-deep-dive-into-android-s-threading-magic-00gkllcus) · droidcon · 0 upvotes · 0 comments

---

Tags: [#android](https://daily.dev/tags/android), [#architecture](https://daily.dev/tags/architecture), [#kotlin](https://daily.dev/tags/kotlin)

[View this post on daily.dev](https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"From viewModelScope to Custom Classes: Safe Coroutine Architecture in Kotlin","url":"https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce"},"datePublished":"2026-03-14T19:47:34.541Z","dateModified":"2026-03-14T19:48:01.835Z","description":"A step-by-step guide to building lifecycle-aware, self-cancelling coroutine owners in Kotlin without relying on Android's viewModelScope. Starting from a...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/473483f1060757516d63d6978618c90d?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/473483f1060757516d63d6978618c90d?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"ProAndroidDev","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"ProAndroidDev","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/656c1eb1f1ef4f91936e86fc16545114","url":"https://daily.dev/sources/pand"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/from-viewmodelscope-to-custom-classes-safe-coroutine-architecture-in-kotlin-hrlkdorce","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"android,architecture,kotlin","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"ProAndroidDev","item":"https://daily.dev/sources/pand"},{"@type":"ListItem","position":3,"name":"From viewModelScope to Custom Classes: Safe Coroutine Architecture in Kotlin"}]}
```

