G'day everyone. Welcome back.
In this video, we'll have a quick look at another way to run code on a background thread.
Kotlin Coroutines provide similar functionality to threads, but they're much more efficient.
The reason is, that creating a new thread takes time. It's quite an expensive operation to perform.
In our code, we only create one thread at a time.
Just to be clear, we haven't introduced any significant performance problems,
and using a thread is fine for our database accesses.
Coroutines can be extremely useful if you're creating lots of threads.
If you were doing it in a loop, for example, then the overhead of creating each thread could become significant.
Coroutines use a shared pool of threads that have already been created.
That means they remove the overhead of creating new threads,
and are great if you're starting background tasks repeatedly.
To switch to using a Coroutine instead of a thread, we just have to replace thread with launch.
Until recently, Coroutines were an experimental feature of Kotlin.
They've now made it into the latest version,
but you will have to make sure that you're using at least version 1.3.20 of Kotlin.
We can check that in the project build.gradle file.
Here we can see that we are using Kotlin version 1.3.21, which is greater than 1.3.20.
Obviously, you can't just change that, if you haven't updated your Kotlin plugin
when Android studio prompts you to,
so make sure you have accepted all the updates, before changing the version in this file.
Next we have to add the Gradle dependency to our build.gradle module file.
Sync it now, and then let's have a look at the changes we need to make in TaskTimerViewModel.
The change is simple. We change thread to globalScope.launch in both places that we've used it;
here
and down here.
Android Studio should have added two imports for us;
kotlinx.coroutines.GlobalScope and kotlinx.coroutines.launch
If it has left the import for Kotlin.concurrent.thread in the imports, we can delete it.
Okay, let's see if it works.
Run the app again, then select the process and task ID and use control F to highlight all occurrences.
The query is being executed on a different thread, and when I delete task two,
that also happens on a background thread.
What you may find this time, is that the thread ID for the delete, is the same as the ID for the subsequent re-query,
or, it may be the same as an earlier thread, when the database was first queried.
With coroutines, threads are reused from a pool of threads,
which reduces the overhead of creating them each time.
You may not get the same thread ID every time of course, that's not guaranteed.
I'm just demonstrating that the threads can be reused, when you use coroutines.
Okay, now that we've got a couple of ways to access the database on a background thread,
we'll clean up our add/edit fragment.
It shouldn't be saving the data.
That function should be moved into the ViewModel.
The database access to save the data should be performed on a background thread as well.
We'll make both those changes in the next video.
Look forward to seeing you there.