Mastering Kotlin Coroutines for Android Development

Mastering Kotlin Coroutines for Android Development

English | MP4 | AVC 1280×720 | AAC 48KHz 2ch | 9.5 Hours | 4.14 GB


Learn how to implement 12+ of the most common use cases for Kotlin Coroutines on Android

Google recommends Kotlin Coroutines as the preferred solution for asynchronous programming on Android. Sooner or later, probably every Android developer will get in touch with them. This course will provide you with a deep understanding of Kotlin Coroutines and shows how to implement 12+ of the most common use cases for Android applications.

At 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 final section of the course 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

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

What you’ll learn

  • How to implement 12+ of the most common use cases for Kotlin Coroutines on Android.
  • Get a deep understanding of Kotlin Coroutines (Suspend Functions, Coroutine Builders, blocking vs suspending, Coroutines vs Threads).
  • Learn advanced Concepts (Structured Concurrency, Coroutine Scopes and Contexts, internal workings)
  • Understand the advantages of using Coroutines over Callbacks or RxJava.
  • How to perform network requests with Retrofit sequentially and concurrently.
  • How to use Room together with Coroutines.
  • How to do background processing with Coroutines.
  • How to appropriately deal with exceptions.
  • How to write Unit Tests for coroutine-based implementations.
  • How to implement Timeouts and Retries.


+ Table of Contents

Introduction
1 The Course Structure
2 How to get the source code of the Sample Project
3 Basic Setup of Sample Project

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

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

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

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

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

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

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

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

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

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

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

Thank you and Bonus
106 Thank you and Bonus