You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Kotlin Coroutines and Flow - Use Cases on Android
4
4
5
-
🎓 Learning Kotlin Coroutines for Android by example.
5
+
🎓 Learning Kotlin Coroutines and Flows for Android Development by example
6
6
7
-
🚀 Sample implementations for real-world Android use cases.
7
+
🚀 Sample implementations for real-world Android use cases
8
8
9
9
🛠 Unit tests included!
10
10
11
-
This repository is intended to be a "Playground Project". You can quickly look up and play around with the different Coroutine Android implementations.
12
-
In the `playground` package you can play around with Coroutines examples that run directly on the JVM.
11
+
This repository is intended to be a "Playground Project". You can quickly look up and play around with the different Coroutine and Flow Android implementations.
12
+
In the `playground` package you can play around with Coroutines and Flow examples that run directly on the JVM.
13
13
14
14
## 🔧 Project Setup
15
15
16
16
Every use case is using its own `Activity` and `JetPack ViewModel`. The `ViewModel`s contain all the interesting Coroutine related code.
17
-
`Activities` listen to `LiveData` events of the `ViewModel` and render received `UiState`s.
17
+
`Activities` listen to `LiveData`or `StateFlow`events of the `ViewModel` and render received `UiState`s.
18
18
19
19
This project is using retrofit/okhttp together with a `MockNetworkInterceptor`. This lets you define how the API should behave.
20
20
Everything can be configured: http status codes, response data and delays. Every use case defines a certain behaviour of the Mock API.
@@ -38,7 +38,7 @@ Unit Tests exist for most use cases.
38
38
* Comparing Kotlin Coroutines with Callbacks and RxJava [[link](https://www.lukaslechner.com/comparing-kotlin-coroutines-with-callbacks-and-rxjava/)]
39
39
* How to run an expensive calculation with Kotlin Coroutines on the Android Main Thread without freezing the UI [[link](https://www.lukaslechner.com/how-to-run-an-expensive-calculation-with-kotlin-coroutines-on-the-android-main-thread-without-freezing-the-ui/)]
40
40
41
-
Sign up to my [newsletter](https://www.lukaslechner.com/newsletter/) to never miss a new blog post. I will publish new blog posts about Coroutines on a regular basis.
41
+
Sign up to my [newsletter](https://www.lukaslechner.com/newsletter/) to never miss new content. I will publish new blog posts and videos about Coroutines and Flow on a regular basis.
42
42
43
43
## 🎓 Online Course
44
44
@@ -54,7 +54,7 @@ If you like this project, please tell other developers about it! ❤️
54
54
55
55
If you like, you can follow me on Twitter [**@LukasLechnerDev**](https://twitter.com/LukasLechnerDev).
56
56
57
-
## ⭐️ Use Cases
57
+
## ⭐️ Coroutine Use Cases
58
58
1.[Perform single network request](#1-perform-single-network-request)
59
59
2.[Perform two sequential network requests](#2-perform-two-sequential-network-requests)
60
60
3.[Perform several network requests concurrently](#3-perform-several-network-requests-concurrently)
@@ -73,7 +73,13 @@ If you like, you can follow me on Twitter [**@LukasLechnerDev**](https://twitter
73
73
16.[Performance analysis of dispatchers, number of coroutines and yielding](#16-performance-analysis-of-dispatchers-number-of-coroutines-and-yielding)
74
74
17.[Perform expensive calculation on Main Thread without freezing the UI](#17-perform-expensive-calculation-on-main-thread-without-freezing-the-ui)
4.[Exposing Flows in the ViewModel](#4-exposing-flows-in-the-viewmodel)
81
+
82
+
## 📄 Coroutine Use Cases Description
77
83
78
84
### 1. Perform single network request
79
85
@@ -226,9 +232,34 @@ See [[this blog post](https://www.lukaslechner.com/how-to-run-an-expensive-calcu
226
232
227
233
You can play around and check the performance of different configurations!
228
234
229
-
## 👷♀️ Contributing 👷♂️
235
+
## 📄 Flow Use Cases Description
236
+
237
+
### 1. Flow Basics
238
+
239
+
This simple use case shows how to consume values from a `DataSource` that emits live stock information and how to display them in the UI.
240
+
241
+
The datasource exposes a flow which is built with the `flow{}` flow builder. It fetches fresh stock information every 5 seconds from a mocked endpoint.
242
+
243
+
A `LiveData` property that exposes the `UiState` in the `ViewModel` is created by using the `.asLiveData()` terminal operator.
244
+
This use case also shows how to use the `map` intermediate operator and the `onStart` lifecycle operator.
245
+
246
+
### 2. Basic Flow Intermediate Operators
247
+
248
+
The second use case is an extension of the first one.
249
+
It uses some basic intermediate operators, like `withIndex`, `map`, `take` and `filter`.
250
+
251
+
### 3. Flow Exception Handling
252
+
253
+
The third use case shows how to properly implement exception handling with flows.
254
+
255
+
It uses the `catch` operator to handle exceptions of our flow in the `ViewModel`, and uses the `retry ` operator to retry failed network requests in the `DataSource`.
256
+
257
+
### 4. Exposing Flows in the ViewModel
258
+
259
+
This use case shows how to expose flows (a `StateFlow` to be precise) in the `ViewModel` instead of a `LiveData` property.
260
+
The `statIn` operator is used to convert the ordinary, cold `Flow` into a hot `StateFlow`.
230
261
231
-
I am currently learning Coroutines myself. So if you have any ideas for or improvements or other use cases, feel free to create a pull request or an issue.
262
+
In the `Activity`, the `repeadOnLifecycle` suspend function is used to collect emissions of the `StateFlow` in a lifecycle-aware manner.
0 commit comments