computation at some point and resume later on • Coroutines are like light-weight threads • Coroutines run on a shared pool of threads, one thread can run many coroutines
later stage without blocking the thread • Add the suspend modifier to the function signature: suspend fun dockToStation() • Can only be called by other suspending functions or coroutines • The compiler converts suspending function to a function with a Continuation
to determine the state of the operation, cancel the operation • ContinuationInterceptor - intercepts the continuation of the coroutine • CoroutineName - user defined, used for debugging • CoroutineExceptionHandler - handles all exceptions in the coroutine context
the coroutines are dispatched on the right threads • Dispatcher.Default - used for CPU intensive tasks • Dispatchers.IO - used for IO intensive operations • Dispatchers.Unconfined - Starts in calling thread, resumes in thread determined by the suspending function • Dispatchers.Main - used for execution in the main thread
• Inherits the context from the scope it was invoked in • Scope controls the lifecycle, should be implemented on components that have a lifecycle of their own • Examples are launch,async,runBlocking
Returns a Deferred, which can be used to cancel the coroutine • Deferred has an await() function, which will wait until the result is available and then return it • The await() function is also a suspending function
Subramanium https://youtu.be/jT2gHPQ4Z1Q • Async Operations with Kotlin Coroutines (Part 1 and 2) by Mayowa Adegeye http://bit.ly/2KorsUO • Follow Roman Elizarov on Twitter @relizarov and Medium @elizarov