title: How to use coroutines in a Kotlin Library
body: <p>how do i correctly use coroutines in a Library class which has nothing to do with the activity lifecycle?</p>
  <p>For now, i created a private property in my class:</p>
  <pre><code>private val coroutineScope = CoroutineScope(Dispatchers.Main)
  </code></pre>
  <p>and have a cancel method, to cancel the scope.</p>
  <pre><code> public fun cancel() {
          coroutineScope.coroutineContext.cancelChildren()
   }
  </code></pre>
  <p>Is there any cleaner way to make this work, without having to call cancel on my library class in onPause/OnStop?</p>
  <p>EDIT:</p>
  <p>Also, an additional question: Does it matter, if my created CoroutineContext is in a Singleton? Like this:</p>
  <pre><code>public object MyObject {
     
      private val coroutineScope = CoroutineScope(Dispatchers.Main)
  }
  </code></pre>
  <p>Is there any danger of memory leaks or similar?</p>