title: What is the difference between coroutineScope { launch { code } } and withContext(iODispatcher) { code } in the Architecture Sample by Google?
body: <p>Code: <a href="https://github.com/android/architecture-samples/blob/main/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepository.kt" rel="nofollow noreferrer">https://github.com/android/architecture-samples/blob/main/app/src/main/java/com/example/android/architecture/blueprints/todoapp/data/source/DefaultTasksRepository.kt</a></p>
  <p>Snippet from the above Code link:</p>
  <pre class="lang-kotlin prettyprint-override"><code>    override suspend fun activateTask(taskId: String) {
          withContext(ioDispatcher) {
              (getTaskWithId(taskId) as? Success)?.let { it -&gt;
                  activateTask(it.data)
              }
          }
      }
  
      override suspend fun clearCompletedTasks() {
          coroutineScope {
              launch { tasksRemoteDataSource.clearCompletedTasks() }
              launch { tasksLocalDataSource.clearCompletedTasks() }
          }
      }
  
      override suspend fun deleteAllTasks() {
          withContext(ioDispatcher) {
              coroutineScope {
                  launch { tasksRemoteDataSource.deleteAllTasks() }
                  launch { tasksLocalDataSource.deleteAllTasks() }
              }
          }
      }
  
      override suspend fun deleteTask(taskId: String) {
          coroutineScope {
              launch { tasksRemoteDataSource.deleteTask(taskId) }
              launch { tasksLocalDataSource.deleteTask(taskId) }
          }
      }
  </code></pre>
  <p>When to use which one?</p>
  <p>Sometimes the <code>coroutineScope { launch { code } }</code> is inside <code>withContext(iODispatcher)</code>!</p>
  <p>When to use: <code>coroutineScope { launch { code } }</code></p>
  <p>When to use: <code>withContext(iODispatcher)</code></p>
  <p>When to use them nested: <code>coroutineScope { launch { code } }</code> is inside <code>withContext(iODispatcher)</code></p>