1
00:00:05,500 --> 00:00:06,800
Welcome back, everyone.

2
00:00:06,800 --> 00:00:11,300
Did I really say we were going to start improving our app at the end of the last video?

3
00:00:11,960 --> 00:00:16,260
Well, we were but then Google released version 2.2.0

4
00:00:16,510 --> 00:00:19,870
of lifecycle-viewmodel-ktx.

5
00:00:21,070 --> 00:00:24,430
One of the new features concerns coroutines in a ViewModel.

6
00:00:25,330 --> 00:00:28,330
As we are using coroutines in our ViewModels,

7
00:00:28,630 --> 00:00:30,730
we've added these next few videos.

8
00:00:31,720 --> 00:00:34,620
We originally changed the app to use coroutines

9
00:00:34,620 --> 00:00:36,220
while they were still experimental.

10
00:00:37,100 --> 00:00:39,460
Then they became part of the Kotlin language,

11
00:00:39,460 --> 00:00:44,160
no longer experimental and Jetbrains have been improving them ever since.

12
00:00:45,060 --> 00:00:47,360
One thing you'll now find online

13
00:00:47,660 --> 00:00:50,660
is a suggestion that using global scope

14
00:00:50,660 --> 00:00:52,260
isn't always a good idea.

15
00:00:53,560 --> 00:00:56,560
Let's have a look at the code, so you can see what I'm talking about.

16
00:00:57,260 --> 00:00:59,560
I'll start with the LoadData function

17
00:00:59,560 --> 00:01:02,160
in our DurationsViewModel class.

18
00:01:04,660 --> 00:01:06,660
On line 191,

19
00:01:06,660 --> 00:01:10,260
we use GlobalScope to launch our coroutine.

20
00:01:10,860 --> 00:01:13,060
That's not necessarily a problem here

21
00:01:13,460 --> 00:01:17,060
because we've only got a single function running asynchronously.

22
00:01:17,720 --> 00:01:20,020
For a discussion about using GlobalScope,

23
00:01:20,420 --> 00:01:23,820
check out a blog post by one of the JetBrains team leads

24
00:01:23,820 --> 00:01:26,820
Roman Elizarov on medium.com.

25
00:01:27,810 --> 00:01:30,410
I'll switch to that page in my browser,

26
00:01:30,410 --> 00:01:33,010
and the link will be in the resources for this video.

27
00:01:35,810 --> 00:01:39,170
It's worth reading this article and some of the others it links to

28
00:01:39,170 --> 00:01:41,870
if you need to understand co routines more fully.

29
00:01:42,370 --> 00:01:46,070
For our purposes, running single functions in the background,

30
00:01:46,070 --> 00:01:48,370
we don't need to go into that level of detail.

31
00:01:49,170 --> 00:01:50,670
At the end of the article,

32
00:01:50,670 --> 00:01:53,670
you'll find a link to Google's coroutines code lab

33
00:01:54,030 --> 00:01:55,630
if you want to learn more about them.

34
00:01:56,620 --> 00:01:58,980
If you're launching coroutines in a ViewModel,

35
00:01:58,980 --> 00:02:01,780
you can now specify ViewModelScope.

36
00:02:02,440 --> 00:02:05,040
ViewModelScope is an extension property

37
00:02:05,040 --> 00:02:07,840
that was added in version 2.2.0

38
00:02:07,840 --> 00:02:10,199
of the androidx lifecycle components

39
00:02:10,699 --> 00:02:14,500
with the stable version being released at the end of January 2020.

40
00:02:15,760 --> 00:02:17,560
To use ViewModelScope,

41
00:02:17,560 --> 00:02:20,760
we need to add another dependency to build.gradle.

42
00:03:02,560 --> 00:03:04,060
Remember to sync now

43
00:03:05,660 --> 00:03:09,260
before attempting to reference this component in your code. Okay.

44
00:03:09,560 --> 00:03:11,360
Back to the DurationsViewModel.

45
00:03:13,260 --> 00:03:17,460
We can now use ViewModelScope rather than GlobalScope.

46
00:03:33,460 --> 00:03:36,360
I'll talk a bit about that Dispatcher later.

47
00:03:36,760 --> 00:03:39,860
We make the same change in the DeleteRecords function

48
00:03:39,860 --> 00:03:41,860
on line 214.

49
00:03:55,960 --> 00:03:58,960
We launch a coroutine in five places

50
00:03:58,960 --> 00:04:00,660
in TaskTimerViewModel.

51
00:04:01,360 --> 00:04:03,620
The first is in the LoadTasks function.

52
00:04:23,820 --> 00:04:27,020
I'll copy that line to the clipboard and then paste it in,

53
00:04:27,020 --> 00:04:28,420
in the other four places.

54
00:04:32,420 --> 00:04:35,120
There are two in the SaveTask function.

55
00:04:47,620 --> 00:04:49,920
There's another one in DeleteTask

56
00:04:56,520 --> 00:04:59,220
and the last one is in the SaveTiming function.

57
00:05:07,220 --> 00:05:11,220
Using ViewModelScope now that Google has made it available

58
00:05:11,220 --> 00:05:13,120
is a good idea in our ViewModel.

59
00:05:13,620 --> 00:05:15,420
One advantage it provides

60
00:05:15,420 --> 00:05:18,620
is that the coroutines will automatically be cancelled

61
00:05:18,620 --> 00:05:20,420
if the ViewModel is destroyed.

62
00:05:21,220 --> 00:05:23,880
That's not something we really have to worry about in this app.

63
00:05:24,480 --> 00:05:28,880
We're accessing a local database, and we're not performing massive bulk updates.

64
00:05:29,880 --> 00:05:32,980
But if you're downloading data from the internet, for example,

65
00:05:32,980 --> 00:05:35,280
then it would be something you would want to deal with. 

66
00:05:36,180 --> 00:05:39,180
Now that Google have added ViewModelScope, it's easy.

67
00:05:39,730 --> 00:05:43,530
ViewModelScope takes care of cancelling our coroutines for us.

68
00:05:44,430 --> 00:05:46,730
I'll come back to this point in the next video.

69
00:05:47,230 --> 00:05:49,830
I'll start that video by testing the change and

70
00:05:50,230 --> 00:05:53,230
then we'll discuss that dispatcher. I'll see you there.

