Welcome back, everyone.
Did I really say we were going to start improving our app at the end of the last video?
Well, we were but then Google released version 2.2.0
of lifecycle-viewmodel-ktx.
One of the new features concerns coroutines in a ViewModel.
As we are using coroutines in our ViewModels,
we've added these next few videos.
We originally changed the app to use coroutines
while they were still experimental.
Then they became part of the Kotlin language,
no longer experimental and Jetbrains have been improving them ever since.
One thing you'll now find online
is a suggestion that using global scope
isn't always a good idea.
Let's have a look at the code, so you can see what I'm talking about.
I'll start with the LoadData function
in our DurationsViewModel class.
On line 191,
we use GlobalScope to launch our coroutine.
That's not necessarily a problem here
because we've only got a single function running asynchronously.
For a discussion about using GlobalScope,
check out a blog post by one of the JetBrains team leads
Roman Elizarov on medium.com.
I'll switch to that page in my browser,
and the link will be in the resources for this video.
It's worth reading this article and some of the others it links to
if you need to understand co routines more fully.
For our purposes, running single functions in the background,
we don't need to go into that level of detail.
At the end of the article,
you'll find a link to Google's coroutines code lab
if you want to learn more about them.
If you're launching coroutines in a ViewModel,
you can now specify ViewModelScope.
ViewModelScope is an extension property
that was added in version 2.2.0
of the androidx lifecycle components
with the stable version being released at the end of January 2020.
To use ViewModelScope,
we need to add another dependency to build.gradle.
Remember to sync now
before attempting to reference this component in your code. Okay.
Back to the DurationsViewModel.
We can now use ViewModelScope rather than GlobalScope.
I'll talk a bit about that Dispatcher later.
We make the same change in the DeleteRecords function
on line 214.
We launch a coroutine in five places
in TaskTimerViewModel.
The first is in the LoadTasks function.
I'll copy that line to the clipboard and then paste it in,
in the other four places.
There are two in the SaveTask function.
There's another one in DeleteTask
and the last one is in the SaveTiming function.
Using ViewModelScope now that Google has made it available
is a good idea in our ViewModel.
One advantage it provides
is that the coroutines will automatically be cancelled
if the ViewModel is destroyed.
That's not something we really have to worry about in this app.
We're accessing a local database, and we're not performing massive bulk updates.
But if you're downloading data from the internet, for example,
then it would be something you would want to deal with.
Now that Google have added ViewModelScope, it's easy.
ViewModelScope takes care of cancelling our coroutines for us.
I'll come back to this point in the next video.
I'll start that video by testing the change and
then we'll discuss that dispatcher. I'll see you there.