Skip to content

mustafacan0225/CoroutineSamples

Repository files navigation

Coroutine Samples - Jetpack Compose - MVI(MVVM with state management)

  • Dispatchers
  • Parallel Call With Job
  • Parallel Call With Async
  • Async With coroutineScope
  • Exception With Job
  • Exception With SupervisorJob
  • Exception With supervisorScope
  • Exception With coroutineScope
  • Job Cancellation
  • Parent Job Cancellation
  • invokeOnCompletion
  • withContext Sample
  • Multiple withContext
  • withContext(NonCancellable)
Note: Since this application focuses on coroutines, the coroutine codes are located in the viewmodel, but in real scenarios they may be located in the data layer or in different classes depending on the architectural structure of the project.

Dispatchers: Which dispatcher runs on which thread?
Parallel Call With Job
Parallel Call With Async
Async With coroutineScope
Exception With Job
Since an error occurred in the child jobs, other ongoing child jobs are cancelled(For example: Dogs Content).
Exception With SupervisorJob
Although there was an error in the child job, the other child job was not cancelled and was completed. Because SupervisorJob was used.
Exception With coroutineScope
Since an error occurred in the child jobs, other ongoing child jobs are cancelled before completion(For example: Dogs Content).
Exception With supervisorScope
Although there was an error in the child job, the other child job was not cancelled and was completed. Because supervisorScope was used.
Job Cancellation
In this example, the job for cats will be canceled 1.5 seconds after the button is clicked. Since the job for dogs was not canceled, that job will be completed.
Parent Job Cancellation
In this example, the parent job will be canceled 1.5 seconds after the button is clicked. Since the parent job is canceled, all child jobs are canceled.
withContext(NonCancellable)
We should use withContext(NonCancellable) for all suspend calls that need to be executed even when a coroutine is in a "Cancelling" state.
invokeOnCompletion
Another way to handle coroutine cancellation or completion is to use the invokeOnCompletion function from Job, which is used to set a handler to be called when the job reaches a terminal state, namely either "Completed" or "Cancelled".
withContext Sample
Multiple withContext
Multiple withContext, runs sequentially.

About

Coroutine - Jetpack Compose - MVI Architecture

Topics

Resources

Stars

Watchers

Forks

Languages