A Journey from
Callback Hell
to
Kotlin Coroutines
Ahmed Nabil
Kotlin Egypt
@AhmedNMahran
Fri. Oct 21 2022
About Me
About Me
● Writing Code since I was 11
● ~10 years in Mobile App development
● Given talks to thousands of audiences
● Prev. Android Mentor at 1M Arab Coders.
● Worked at Hungerstation,Codly, and others.
● Software Tech. Lead at Vodafone (_VOIS).
● Head of Kotlin Egypt.
● Content creator (videos/articles)
Fri. Oct 21 2022
About Me
What
is Kotlin ?
Fri. Oct 21 2022
About Me
Kotlin
● A modern Programming Language.
● Developed by Jetbrains & Open-Source community
● Since 2010.
● Current Stable Version is 1.7.20 (as of 20-10-2022)
Fri. Oct 21 2022
About Me
Used to develop
● Multiplatform-Mobile
● Server-Side
● Web Front-End
● Android
● WatchOS
● Data Science
● and more
Fri. Oct 21 2022
About Me
Let
the journey
Begin
Fri. Oct 21 2022
Fri. Oct 21 2022
Fri. Oct 21 2022
About Me
ANR
● any operation > 16ms
may cause Lag
● I/O operations,
long-running operation
● Main thread should ONLY
do UI operations
Fri. Oct 21 2022
Fri. Oct 21 2022
About Me
Problem
Code running in Main
thread causing Crash or
Lag.
Fri. Oct 21 2022
About Me
Asynchronous
Code
Fri. Oct 21 2022
About Me
Approaches
● Threading
● Callbacks
● Futures, Promises
● Reactive Extensions (Rx)
● Coroutines
Fri. Oct 21 2022
About Me
Threads
A program splits itself
into 2 or more running
tasks
Fri. Oct 21 2022
About Me
Threads Limitations
● Expensive
● Not Infinite, limited with device capabilities
● Not always available, (e.g. JS doesn’t have threading)
● Hard to Error-handle and debug
Fri. Oct 21 2022
Fri. Oct 21 2022
About Me
Callbacks
a function sent as an
argument to another
function to be executed
at certain point in time.
Fri. Oct 21 2022
Fri. Oct 21 2022
About Me
Callback Limitations
● Nested Callbacks not easy to understand
● Hard to Error-handle and debug
Fri. Oct 21 2022
Fri. Oct 21 2022
About Me
Futures
and Promises
I promise I’ll return at
some point in the future.
Fri. Oct 21 2022
About Me
Limitations
● Nested Callbacks not easy to understand
● Return Type :
Needs changing the models (extra steps in getting the
data)
● Hard to Error-handle and debug
Fri. Oct 21 2022
About Me
Rx
Everything is a stream
and it’s observable
Needs learning new apis
:(
Fri. Oct 21 2022
About Me
Coroutines
Kotlin’s approach to work
with async. code
Fri. Oct 21 2022
Process
Thread1
Thread2
Coroutine1
Coroutine2
Coroutine3
Couroutine4
Fri. Oct 21 2022
About Me
Coroutines
● No new apis needed, same language used (kotlin)
● Return type doesn’t change.
● Write sequential code while being thread-safe
● Lightweight, can run many of them in one thread,
● Easy to switch between threads
● Platform independent
● Easy to debug and detect errors.
Fri. Oct 21 2022
Fri. Oct 21 2022
Don’t Miss
My Workshop
6:00 PM today
Fri. Oct 21 2022
About Me
Follow Me
@AhmedNMahran
Thanks!
Youtube
(contains all my links)
Fri. Oct 21 2022
Thanks
Ahmed Nabil
Kotlin Egypt
@AhmedNMahran
Fri. Oct 21 2022

DroidConEgypt-21-10-2022-Coroutines-AhmedNabil.pdf