Share
Embed
Async Operations with Kotlin Coroutines
Explains how we currently do async operations in Android and how kotlin coroutines helps to write asynchronous code in a synchronous manner.
Adegeye Mayowa
October 05, 2018
Transcript
112 slides
-
fun login(username: String, password: String): User fun getOrders(userId: String): List<Orders>
fun fetchUserOrders() { val user = login(username, password) val orders = getOrders(user.userId) showUserOrders(orders) } -
fun fetchUserOrders(username: String, password: String) { login(username, password) { user
-> getOrders(user.userId) { orders -> showUserOrders(orders) } } } Callbacks -
RxJava Example fun fetchUserOrders(username: String, password: String) { login(username, password)
.flatMap { user → getOrders(user.userId) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.main()) .subscribe({orders -> showUserOrders(orders)}) } -
What we actually want fun fetchUserOrders() { val user =
login(username, password) val orders = getOrders(user.userId) showUserOrders(orders) } -
What is a coroutine A lightweight thread that can run
a computation without blocking. It can be suspended and resumed at a later time. -
KEY CONCEPTS • SUSPENDING FUNCTIONS • COROUTINE CONTEXT / COROUTINE
DISPATCHER • COROUTINE BUILDERS -
SUSPENDING FUNCTION A function that can be started, paused and
resumed at a later time. -
SUSPENDING FUNCTION A function that can be started, paused and
resumed at a later time. • Can only be called from a coroutine and from another suspending function -
SUSPENDING FUNCTION A function that can be started, paused and
resumed at a later time. • Can only be called from a coroutine and from another suspending function • Can take parameters and have return types. -
fun doHeavyComputation(param: Int, callback:Continuation<Int>) interface Continuation<in T> { val context:
CoroutineContext fun resume(value: T) { } fun resumeWithException(exception: Throwable) } -
COROUTINE CONTEXT Coroutine Context: A map from CoroutineContext.Key to CoroutineContext.Element
• Job • ContinuationInterceptor -
COROUTINE CONTEXT Coroutine Context: A map from CoroutineContext.Key to CoroutineContext.Element
• Job • ContinuationInterceptor • CoroutineName -
COROUTINE CONTEXT Coroutine Context: A map from CoroutineContext.Key to CoroutineContext.Element
• Job • ContinuationInterceptor • CoroutineName • CoroutineExceptionHandler -
COROUTINE DISPATCHERS Determines which thread or threads the coroutine uses
for its execution. It can confine a coroutine to specific thread, a thread pool or let it run unconfined. -
Coroutine Dispatchers • Dispatchers.DEFAULT • Dispatchers.UNCONFINED • Dispatchers.IO • Dispatchers.MAIN
• newFixedThreadPoolContext / newSingleThreadContext -
Launch fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, Start: CoroutineStart =
CoroutineStart.DEFAULT, onCompletion: CompletionHandler? = null, block: suspend CoroutineScope.() -> Unit ): Job -
Example suspend fun fibonacciN(n: Int): Int {...} val job =
GlobalScope.launch { val fib = fibonacciN(20) println(“20th fibonacci is $fib”) } -
fun <T> CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart =
CoroutineStart.DEFAULT, onCompletion: CompletionHandler? = null block: suspend CoroutineScope.() -> T ): Deferred<T> Async -
Async... • You need to call await() on the returned
Deferred<T> object to get the eventual result. -
Async... • You need to call await() on the returned
Deferred<T> object to get the actual result. • Async..await be used to run tasks in parallel. -
Async... • You need to call await() on the returned
Deferred<T> object to get the actual result. • Async..await be used to run tasks in parallel. • Deferred<T> is also a job. -
Parallel operations example suspend fun loadImage(url: String): Bitmap fun combineImages()=
GlobalScope.launch(Dispatchers.MAIN) { val image1 = async(Dispatchers.DEFAULT) { loadImage(“..”) } val image2 = async(Dispatchers.DEFAULT) { loadImage(“..”) } imageView.setImageBitmap( combine(image1.await(), image2.await()) ) } -
Parallel operations example suspend fun loadImage(url: String): Bitmap fun combineImages()=
GlobalScope.launch(Dispatchers.MAIN) { val image1 = async(Dispatchers.DEFAULT) { loadImage(“..”) } val image2 = async(Dispatchers.DEFAULT) { loadImage(“..”) } imageView.setImageBitmap( combine(image1.await(), image2.await()) ) } -
private fun loadData() = GlobalScope.launch(Dispatchers.MAIN) { view.showLoading() val result =
withContext(Dispatcher.IO) { dataRepository.loadData() } } -
private fun loadData() = GlobalScope.launch(Dispatchers.MAIN) { view.showLoading() val result =
withContext(Dispatcher.IO) { dataRepository.loadData() } } -
private fun loadData() = GlobalScope.launch(Dispatchers.MAIN) { view.showLoading() val result =
withContext(Dispatcher.IO) { dataRepository.loadData() } view.hideLoading() } -
private fun loadData() = GlobalScope.launch(Dispatchers.MAIN) { view.showLoading() val result =
withContext(Dispatcher.IO) { dataRepository.loadData() } view.hideLoading() view.showData(result) } -
get user orders revisited fun fetchUserOrders() = GlobalScope.launch(Dispatchers.MAIN){ val user
= withContext(Dispatchers.IO) { login(username, password) } val orders = withContext(Dispatchers.IO) { getOrders(user.userId) } showUserOrders(orders) } -
CANCELLING A JOB Call cancel() on the Job object returned
from a single coroutine -
CANCELLING A JOB Call cancel() on the Job object returned
from a single coroutine val job = GlobalScope.launch { // background operation } job.cancel() -
Child Coroutines inherits the context of parent coroutines val job
= GlobalScope.launch { val task1 = async {...} val task2 = async {...} val result = task1.await() + task2.await() } -
Child Coroutines inherits the context of parent coroutines val job
= GlobalScope.launch { val task1 = async {...} val task2 = async {...} val result = task1.await() + task2.await() } job.cancel() //cancels the execution of parent and child coroutines -
class MainActivity: AppCompatActivity() { private fun loadData() { } private
fun doAnimations() { } } -
class MainActivity: AppCompatActivity(), CoroutineScope { private fun loadData() { }
private fun doAnimations() { } } -
class MainActivity: AppCompatActivity(), CoroutineScope { override val coroutineContext: CoroutineContext get()
= Dispatchers.DEFAULT private fun loadData() { } private fun doAnimations() { } } -
class MainActivity: AppCompatActivity(), CoroutineScope { private lateint var job override
val coroutineContext: CoroutineContext get() = Dispatchers.DEFAULT private fun loadData() { } private fun doAnimations() { } } -
class MainActivity: AppCompatActivity(), CoroutineScope { private lateint var job override
val coroutineContext: CoroutineContext get() = Dispatchers.DEFAULT override fun onCreate(savedInstanceState: Bundle?) { job = Job() } private fun loadData() { } } -
class MainActivity: AppCompatActivity(), CoroutineScope { private lateint var job override
val coroutineContext: CoroutineContext get() = Dispatchers.DEFAULT override fun onCreate(savedInstanceState: Bundle?) { job = Job() } override fun onDestroy() { job.cancel() } } -
class MainActivity: AppCompatActivity(), CoroutineScope { private lateint var job override
val coroutineContext: CoroutineContext get() = Dispatchers.DEFAULT + job override fun onCreate(savedInstanceState: Bundle?) { job = Job() } override fun onDestroy() { job.cancel() } } -
class MainActivity: AppCompatActivity(), CoroutineScope { private lateint var job override
val coroutineContext: CoroutineContext get() = Dispatchers.DEFAULT + job override fun onCreate(savedInstanceState: Bundle?) { job = Job() } override fun onDestroy() { job.cancel() } } -
HANDLING EXCEPTIONS Exceptions can be handled in the conventional way
by wrapping code in try..catch block. -
HANDLING EXCEPTIONS Exceptions can be handled in the conventional way
by wrapping code in try..catch block. Coroutine builders either propagate exceptions automatically or expose it to the user -
fun main(args: Array<String>) = runBlocking { val job = GlobalScope.launch
{ println("Throwing exception from launch") throw IndexOutOfBoundsException() } job.join() println("Joined failed job") val deferred = GlobalScope.async { println("Throwing exception from async") throw ArithmeticException() } try { deferred.await() println("Unreached") } catch (e: ArithmeticException) { println("Caught ArithmeticException") } } -
fun main(args: Array<String>) = runBlocking { val job = GlobalScope.launch
{ println("Throwing exception from launch") throw IndexOutOfBoundsException() } job.join() println("Joined failed job") val deferred = GlobalScope.async { println("Throwing exception from async") throw ArithmeticException() } try { deferred.await() println("Unreached") } catch (e: ArithmeticException) { println("Caught ArithmeticException") } } -
fun main(args: Array<String>) = runBlocking { val job = GlobalScope.launch
{ println("Throwing exception from launch") throw IndexOutOfBoundsException() // exception printed out to the console } job.join() println("Joined failed job") val deferred = GlobalScope.async { println("Throwing exception from async") throw ArithmeticException() //nothing happens, waits for user to call await } try { deferred.await() println("Unreached") } catch (e: ArithmeticException) { println("Caught ArithmeticException") } } -
fun main(args: Array<String>) = runBlocking { val job = GlobalScope.launch
{ println("Throwing exception from launch") throw IndexOutOfBoundsException() // exception printed out to the console } job.join() println("Joined failed job") val deferred = GlobalScope.async { println("Throwing exception from async") throw ArithmeticException() //nothing happens, waits for user to call await } try { deferred.await() println("Unreached") // will not be printed } catch (e: ArithmeticException) { println("Caught ArithmeticException") } } -
CoroutineExceptionHandler val exceptionHandler = CoroutineExceptionHandler { _, exception -> print(“Caught:
$exception") } runBlocking { val job = GlobalScope.launch(exceptionHandler) { throw IOException(“error occurred) } job.join() } //prints: caught: java.io.IOException: error occurred -
• Provides a way to transfer a stream of values
• They can be shared between different coroutines and have default capacity of 1. -
• Provides a way to transfer a stream of values
• They can be shared between different coroutines and have default capacity of 1. • Channels implements two interfaces, the SendChannel<E> and the ReceiveChannel<E> -
public interface SendChannel<E> { suspend fun send(element: E) fun offer(element:
E) fun close(cause: Throwable? = null) : Boolean } -
public interface SendChannel<E> { suspend fun send(element: E) fun offer(element:
E) fun close(cause: Throwable? = null) : Boolean } public interface ReceiveChannel<E> { suspend fun receive(): E fun close(throwable? = null): Boolean } -
USAGE val channel = Channel<Int>() //default size is 1 GlobalScope.launch
{ for (i in 1..5) { delay(100) channel.send(i) } channel.close() } -
USAGE val channel = Channel<Int>() //default size is 1 GlobalScope.launch
{ for (i in 1..5) { delay(100) channel.send(i) } channel.close() } -
USAGE val channel = Channel<Int>() //default size is 1 GlobalScope.launch
{ for (i in 1..5) { delay(100) channel.send(i) } channel.close() } GlobalScope.launch { for (i in channel) println(“received: $i”) } -
Producer, Publisher and Actor • produce<E> {} - streams of
event • publish<E> {} - becomes a stream of event upon subscription -
Producer, Publisher and Actor • produce<E> {} - streams of
event • publish<E> {} - becomes a stream of event upon subscription • actor<E> - implements SendChannel<E> -
RxJava InterOp Call `openForSubscription` extension function on Observable / Flowable
Flowable.range(1, 5).openForSubscription().consume { for (i in this) println(i) // ReceiveChannel<Int> } -
RxJava InterOp Call `openForSubscription` extension function on Observable / Flowable
Flowable.range(1, 5).openForSubscription().consume { for (i in this) println(i) // ReceiveChannel<Int> } Or Flowable.range(1, 5).consumeEach{ println($it) } -
Convert Job to Rx Completable val job = GlobalScope.launch {
heavyComputation() } -
Convert Job to Rx Completable val job = GlobalScope.launch {
heavyComputation() } job.asCompletable(Dispatchers.Default) -
Convert Job to Rx Completable val job = GlobalScope.launch {
heavyComputation() } job.asCompletable(Dispatchers.Default).subscribe({..},{.. }) -
Convert Deferred to Rx Single suspend fun loadData(): List<Item> {
} val deferred = GlobalScope.async { loadData() } deferred.asSingle(Dispatcher.DEFAULT).subscribe({ //on success }, { //on error }) -
RxJava Coroutine Builders rxObservable { } rxFlowable { } rxCompletable
{ } rxSingle { } rxMaybe { } -
ANDROID INTEGRATION kotlin { experimental { coroutines ‘enable’ } }
implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-core:0.26.1’ -
ANDROID INTEGRATION kotlin { experimental { coroutines ‘enable’ } }
implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-core:0.26.1’ implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-android:0.26.1’ -
ANDROID INTEGRATION kotlin { experimental { coroutines ‘enable’ } }
implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-core:0.26.1’ implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-android:0.26.1’ implementation ‘org.jetbrains.kotlinx:kotlinx-coroutines-rx2:0.26.1’ -
Retrofit Integration val retrofit = Retrofit.Builder() .baseUrl(“http://apiurl.com/”) .addCallAdapterFactory(CoroutineCallAdapterFactory()) .build(); //gradle
implementation “com.jakewharton.retrofit:retrofit2-kotlin-coroutine-experimental-adapter:1.0.0” implementation “com.jakewharton.retrofit:retrofit2-kotlin-coroutine-adapter:0.9.2” -
interface MyService { @GET(“/user”) fun getUser(): Deferred<User> or @GET(“/user”) fun
getUser(): Deferred<Response<User>> }