Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Demystifying Coroutines Frank Tamre @tamrefrank github.com/tamzi
Slide 2
Slide 2 text
2.Definition of Coroutines 3. Using Coroutines 4. Kotlin Coroutines 5. Code sample 1. Brief History of Coroutines Agenda
Slide 3
Slide 3 text
Brief History of Coroutines
Slide 4
Slide 4 text
ERA 1101 1950
Slide 5
Slide 5 text
Ferranti Mark I 1951
Slide 6
Slide 6 text
Grace Hoper and A-0 1952
Slide 7
Slide 7 text
Manchester TC 1953
Slide 8
Slide 8 text
Direct keyboard input to computers 1956
Slide 9
Slide 9 text
Fotran introduced Math-Matic 1957
Slide 10
Slide 10 text
1958
Slide 11
Slide 11 text
LISP Fotran II ALGOL 58
Slide 12
Slide 12 text
Melvin Conway
Slide 13
Slide 13 text
Origins Brief History of coroutines
Slide 14
Slide 14 text
Definition Of Coroutines
Slide 15
Slide 15 text
instance of suspendable computation.
Slide 16
Slide 16 text
Using Coroutines
Slide 17
Slide 17 text
Non-PreEmptive multitasking
Slide 18
Slide 18 text
Non-PreEmptive multitasking
Slide 19
Slide 19 text
Exceptions
Slide 20
Slide 20 text
Event Loops
Slide 21
Slide 21 text
iterators
Slide 22
Slide 22 text
Infinite Lists
Slide 23
Slide 23 text
pipes
Slide 24
Slide 24 text
Handle concurrency in Android
Slide 25
Slide 25 text
Kotlin Coroutines
Slide 26
Slide 26 text
kotlinx-coroutines library
Slide 27
Slide 27 text
STEPS
Slide 28
Slide 28 text
1. Import kotlinx- coroutines library
Slide 29
Slide 29 text
2.Go to that function and create a suspend instance: fun suspendableLambda(block: suspend () -> Unit)
Slide 30
Slide 30 text
suspend fun validateEntries(str: String): Boolean = coroutineScope { val deferred1 = async { firstValidation(str) } val deferred2 = async { secondValidation(str) } deferred1.await() && deferred2.await() }
Slide 31
Slide 31 text
Coroutine builders
Slide 32
Slide 32 text
Launch{} async{} runBlocking{}
Slide 33
Slide 33 text
Important Activity Lifecycle
Slide 34
Slide 34 text
Code sample
Slide 35
Slide 35 text
1 simple DB transactional call
Slide 36
Slide 36 text
fun
inTransaction(f: (Connection) -> CompletableFuture
) : CompletableFuture
{ return this.sendQuery("BEGIN").flatMap { _ -> val p = CompletableFuture
() f(this).onComplete { ty1 -> sendQuery( if (ty1.isFailure) "ROLLBACK" else "COMMIT") .onComplete { ty2 -> if (ty2.isFailure && ty1.isSuccess) p.failed((ty2 as Failure).exception) else p.complete(ty1) } } p } } With Callbacks
Slide 37
Slide 37 text
suspend fun
inTransaction( f: suspend (Connection) -> A): A { try { this.sendQuery("BEGIN") val result = f(this) this.sendQuery("COMMIT") return result } catch (e: Throwable) { this.sendQuery("ROLLBACK") throw e } } with coroutines
Slide 38
Slide 38 text
2 Challenge ATM
Slide 39
Slide 39 text
Thanks @tamrefrank github.com/tamzi