4. 4 ••
How programming works
Intro
Do something (create a resource)
↓
Wait (for completion)
↓
Do something (send it to backend)
↓
Wait (for the response)
↓
Do something (process response or try again)
↓
…
5. 5 ••
Dealing with long running tasks
Asynchronicity
- Direct style
- max 16 ms
- Blocking the main Thread
- Callback
- Error Handling
- Callback Hell
- Futures (RxJava, Promises, …)
- Idea: we wrap the result
- Pro: nice code, errors, good looking
- cons: complex
- Coroutines
- Direct style
6. 6 ••
What is a Coroutine?
- It’s an instance of a suspendable computation
- General concept was already created in 1958
- Similar to a very lightweight Thread – cheap in creation
- Looks like regular blocking code
- Coroutines give us a way to write code that feels imperative, even if it is asynchronous.
7. 7 •• ••
Suspend functions
- A function ‘super powers’
- It’s a function which can suspend and return a value later on
8. 8 •• ••
Suspend functions II
- Suspension points
- Many things work out-of-the box
- Loops
- Exceptions
- Higher-Order-Functions
- …