Kotlin Coroutines and Flow for Android Development [2023]

Kotlin Coroutines and Flow for Android Development [2023]

English | MP4 | AVC 1280×720 | AAC 44KHz 2ch | 156 lectures (16h 6m) | 4.56 GB

The Complete Guide! Get a deep understanding of Kotlin Coroutines and Flow to use them successfully in your Android Apps

Google recommends Kotlin Coroutines and Flow as the preferred solution for asynchronous programming on Android. Sooner or later, probably every Android developer will get in touch with these topics.

This course will give you a deep understanding of Kotlin Coroutines and Flow and show how to implement the most common use cases for Android applications.

This course consists of two big parts: The Coroutines part and the Flow part.

Before being able to use Flows in our applications, we first need a solid understanding of Coroutines. That’s why Coroutines are covered first. However, if you already have some experience with Coroutines, then you can also start with the Flow part right away, and jump back to lessons of the Coroutines part whenever needed.

In the part about Coroutines, first, we will take a detailed look at the advantages of Kotlin Coroutines over other approaches for asynchronous programming, like RxJava and Callbacks.

Then, we will talk about some theoretical fundamentals. These include:

  • Routines vs. Coroutines
  • Suspend Functions
  • Coroutines vs. Threads
  • Blocking vs. Suspending
  • Multithreaded Coroutines
  • Internal workings

Next, we will implement some of the most common use cases for Kotlin Coroutines in Android applications. These include:

  • Performing network requests with Retrofit sequentially and concurrently
  • Implementing Timeouts and Retries
  • Using Room with Coroutines
  • Performing background processing with Coroutines
  • Continuing Coroutine execution even when the user leaves the screen.

To improve your learning experience, this course also challenges you with several exercises.

Learning Coroutines can be overwhelming because you need to learn a lot of new concepts. However, we are going to start simple and as our use cases will get more and more complex, we will learn about new concepts step-by-step. These new concepts are:

  • Coroutine Builders (launch, async, runBlocking)
  • Coroutine Context
  • Coroutine Dispatchers
  • Structured Concurrency
  • Coroutine Scopes (viewModelScope, lifecycleScope, GlobalScope)
  • Jobs and SupervisorJobs
  • scoping functions (coroutineScope{} and supervisorScope{})
  • Cooperative Cancellation
  • Non-Cancellable Code

We will also make a deep dive into Exception Handling and discuss concepts like:

  • exception handling with try/catch
  • exception handling with CoroutineExceptionHandlers
  • when to use try/catch and when to use a CoroutineExceptionHandler
  • exception handling in Coroutines started with launch and async
  • exception handling specifics of scoping functions coroutineScope{} and supervisorScope{}

Unit Tests are very important for every codebase. In the course’s final section, we will write unit tests for most of the coroutine-based use cases we implemented earlier. We will discuss concepts like

  • TestCoroutineDispatcher
  • creating a JUnit4 Rule for testing coroutine-based code
  • runBlockingTest{} Coroutine Builder
  • virtual time
  • Testing sequential and concurrent execution
  • TestCoroutineScope

In the part about Kotlin Flow, we first cover all the basics. We will answer the question “What is a Flow?” and then we discuss the benefits and drawbacks of reactive programming.

Afterward, we are going to have a look at different Flow builders and operators:

  • basic flow builders
  • terminal operators
  • terminal operator “launchIn()”
  • terminal operator “asLiveData()”
  • lifecycle operators
  • intermediate operators

In our first real Flow use case, we use a Flow to create a live stock-tracking feature, that uses all the available basic flow components.

In the next module, we will take a look at Exception Handling and Cancellation with Kotlin Flow.

In the following module, you will learn about StateFlow and SharedFlow and the following concepts:

  • how to make Coroutines lifecycle-aware with the “repeatOnLifecycle()” suspend function
  • Hot Flows VS Cold Flows
  • Converting Flows to SharedFlows with the “shareIn()” operator
  • Converting Flows to StateFlows with the “stateIn()” operator
  • When to use SharedFlow and when to use StateFlow

Next, you will learn about Channels, how they differ from hot flows, and when they are useful in Android Applications.

By the end of this course, you will have a fundamental understanding of Kotlin Coroutines and Flows and be able to write readable and maintainable, asynchronous, and multithreaded Android Applications.

Table of Contents

Introduction
1 Extended Course Preview
2 Course Structure and Goals
3 How to get the source code of the Sample Project
4 Basic Setup of Sample Project

Different Approaches for Asynchronous Programming
5 Section Introduction
6 What is Asynchronous Programming
7 Use Case Explanation
8 Callback Implementation
9 RxJava Implementation
10 Coroutines Implementation
11 Comparing Approaches
12 Recap

Coroutine Fundamentals
13 Section Introduction
14 Routines and Coroutines
15 Suspend Functions
16 Coroutines and Threads
17 Blocking VS Suspending
18 Multithreaded Coroutines
19 Internal Workings
20 How delay() works
21 Section Recap

Performing Network Requests sequentially
22 Introduction
23 Adding Coroutines to your project
24 Coroutine Builders Launch and RunBlocking
25 Main-safety
26 Basic error handling with try-catch
27 Exercise 1
28 Exercise 1 Solution
29 Section Recap

Performing Network Requests concurrently
30 Section Introduction
31 UseCase description
32 Implementing UseCase#3 in a sequential way
33 The async coroutine builder
34 Implementing UseCase#3 in a concurrent way
35 Implementing UseCase#4 in a sequential way
36 Exercise 2 Implementing UseCase#4 in a concurrent way
37 Exercise 2 Solution
38 Section Recap

Higher-Order Functions
39 Implementing a timeout
40 Implementing retries
41 Extract retry logic into higher order function
42 Add exponential backoff to retry
43 Exercise 3 Combining retries and timeout
44 Exercise 3 Solution
45 Section Recap

Using Room with Coroutines
46 Section Introduction
47 UseCase description
48 UseCase Implementation
49 Section Recap

Background Processing with Coroutines
50 Section Introduction
51 UseCase explanation
52 Implementation running on Main Thread
53 Coroutine Context
54 Coroutine Dispatchers
55 Using withContext for context switching
56 Coroutine Scope VS Coroutine Context
57 Exercise4 Perform calculation in several Coroutines
58 Exercise 4 Solution
59 Performance Analysis
60 Section Recap

Structured Concurrency and Coroutine Scopes
61 Section Introduction
62 The unhappy path
63 Structured Concurrency
64 Coroutine Scopes
65 Building up the Job Hierarchy
66 Parents wait for Children
67 Cancellation of parent and child jobs
68 Job and SupervisorJob
69 Unstructured Concurrency
70 GlobalScope
71 ViewModelScope
72 LifecycleScope
73 Scoping Functions coroutineScope{} and supervisorScope{}
74 Continue Coroutine execution when the user leaves the screen
75 Section Recap

Coroutines Cancellation
76 Cancelling Coroutines
77 Cooperative Cancellation
78 NonCancellable Code
79 Making UseCase10 cooperative regarding cancellation
80 Section Recap

Coroutines Exception Handling
81 Section Introduction
82 Exception Handling with try-catch
83 Coroutine Exception Handler
84 Try-Catch VS Coroutine Exception Handler
85 launch{} VS async{}
86 Exception Handling specifics of coroutineScope{}
87 Exception Handling specifics of supervisorScope{}
88 Implementation of UseCase13 with try-catch
89 Implementation of UseCase13 with CoroutineExceptionHandler
90 Implementation of UseCase13 – show results even if a child coroutine fails
91 Rethrowing CancellationExceptions
92 Coroutines Exception Handling Cheat Sheet
93 Conference Talk Exception Handling in Kotlin Coroutines
94 Section Recap

Testing Coroutines
95 Section Introduction
96 Important Information regarding the new kotlinx-coroutines-test 1.6 APIs
97 General Unit Testing Approach
98 Using the TestCoroutineDispatcher for our first unit test
99 Testing the unhappy path
100 Creating a JUnit4 Rule for Testing Coroutine-based code
101 Excercise 5 Create a Unit Test for UseCase2
102 Solution Exercise 5
103 runBlockingTest{} and VirtualTime
104 Testing sequential and concurrent execution
105 Testing Timeouts (UseCase5) and Retries (UseCase 6)
106 Testing implementations that use Dispatchers.Default or Dispatchers.IO
107 TestCoroutineScope and Controlling Coroutine Execution
108 Section Recap

Basics of Kotlin Flow
109 Section Introduction
110 What is a Flow
111 Reactive Programming
112 Starting our first Flow Use Case
113 Basic Flow Builders
114 Exposing a Flow in our DataSource
115 Displaying the stock list on the screen
116 Basic Terminal operators
117 Terminal operator launchIn()
118 Using launchIn() in UseCase1
119 Lifecycle Operators
120 Terminal operator asLiveData()
121 Basic Intermediate Operators
122 Exercise Intermediate Operators
123 Exercise Solution
124 Section Recap
125 Slides of the Section

Flow Exception Handling and Cancellation
126 Flow Exception Handling
127 Exception Transparency
128 The retry() Operator
129 Exercise Flow Exception Handling
130 Exercise Solution
131 Flow Cancellation
132 Slides of the Section

StateFlow and SharedFlow
133 Exposing Flows instead of LiveData in the ViewModel
134 Naive Approach – Exposing regular Flows
135 Lifecycle-aware Coroutines with repeatOnLifecycle()
136 Flows are cold
137 SharedFlows are hot
138 Converting Flows to SharedFlows with shareIn()
139 Keeping the upstream Flow alive during configuration changes
140 The replay parameter
141 StateFlow
142 Converting Flows to StateFlows with stateIn()
143 SharedFlow VS StateFlow
144 Section Recap
145 Slides of the Section

Channels
146 Channels for Android Development
147 Single UI Events
148 Slides of the Section

Concurrent Flows
149 Important Information
150 The buffer() operator
151 Dealing with Backpressure (aka BufferOverflow)
152 collectLatest(), mapLatest() and transformLatest()
153 The conflate() operator
154 Buffers in SharedFlows and StateFlows
155 Slides of the Section

Thank you and Bonus
156 Thank you and Bonus

Homepage