1 00:00:04,760 --> 00:00:07,320 G'day everyone. Welcome back. 2 00:00:07,320 --> 00:00:12,560 In this video, we'll have a quick look at another way to run code on a background thread. 3 00:00:12,560 --> 00:00:18,820 Kotlin Coroutines provide similar functionality to threads, but they're much more efficient. 4 00:00:18,820 --> 00:00:25,440 The reason is, that creating a new thread takes time. It's quite an expensive operation to perform. 5 00:00:25,440 --> 00:00:29,300 In our code, we only create one thread at a time. 6 00:00:29,300 --> 00:00:34,240 Just to be clear, we haven't introduced any significant performance problems, 7 00:00:34,240 --> 00:00:38,020 and using a thread is fine for our database accesses. 8 00:00:38,020 --> 00:00:42,940 Coroutines can be extremely useful if you're creating lots of threads. 9 00:00:42,940 --> 00:00:50,360 If you were doing it in a loop, for example, then the overhead of creating each thread could become significant. 10 00:00:50,360 --> 00:00:55,120 Coroutines use a shared pool of threads that have already been created. 11 00:00:55,120 --> 00:00:58,580 That means they remove the overhead of creating new threads, 12 00:00:58,580 --> 00:01:02,860 and are great if you're starting background tasks repeatedly. 13 00:01:02,860 --> 00:01:09,340 To switch to using a Coroutine instead of a thread, we just have to replace thread with launch. 14 00:01:09,340 --> 00:01:14,200 Until recently, Coroutines were an experimental feature of Kotlin. 15 00:01:14,200 --> 00:01:17,660 They've now made it into the latest version, 16 00:01:17,660 --> 00:01:24,440 but you will have to make sure that you're using at least version 1.3.20 of Kotlin. 17 00:01:24,440 --> 00:01:31,780 We can check that in the project build.gradle file. 18 00:01:31,780 --> 00:01:39,240 Here we can see that we are using Kotlin version 1.3.21, which is greater than 1.3.20. 19 00:01:39,240 --> 00:01:44,200 Obviously, you can't just change that, if you haven't updated your Kotlin plugin 20 00:01:44,200 --> 00:01:46,880 when Android studio prompts you to, 21 00:01:46,880 --> 00:01:51,620 so make sure you have accepted all the updates, before changing the version in this file. 22 00:01:51,620 --> 00:02:05,400 Next we have to add the Gradle dependency to our build.gradle module file. 23 00:02:05,400 --> 00:02:16,140 Sync it now, and then let's have a look at the changes we need to make in TaskTimerViewModel. 24 00:02:16,140 --> 00:02:24,140 The change is simple. We change thread to globalScope.launch in both places that we've used it; 25 00:02:24,140 --> 00:02:29,060 here 26 00:02:29,060 --> 00:02:34,940 and down here. 27 00:02:34,940 --> 00:02:38,940 Android Studio should have added two imports for us; 28 00:02:38,940 --> 00:02:46,080 kotlinx.coroutines.GlobalScope and kotlinx.coroutines.launch 29 00:02:46,080 --> 00:02:54,640 If it has left the import for Kotlin.concurrent.thread in the imports, we can delete it. 30 00:02:54,640 --> 00:02:57,120 Okay, let's see if it works. 31 00:02:57,120 --> 00:03:28,840 Run the app again, then select the process and task ID and use control F to highlight all occurrences. 32 00:03:28,840 --> 00:03:33,980 The query is being executed on a different thread, and when I delete task two, 33 00:03:33,980 --> 00:03:42,480 that also happens on a background thread. 34 00:03:42,480 --> 00:03:49,920 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, 35 00:03:49,920 --> 00:03:55,100 or, it may be the same as an earlier thread, when the database was first queried. 36 00:03:55,100 --> 00:03:59,240 With coroutines, threads are reused from a pool of threads, 37 00:03:59,240 --> 00:04:02,820 which reduces the overhead of creating them each time. 38 00:04:02,820 --> 00:04:07,740 You may not get the same thread ID every time of course, that's not guaranteed. 39 00:04:07,740 --> 00:04:13,280 I'm just demonstrating that the threads can be reused, when you use coroutines. 40 00:04:13,280 --> 00:04:18,800 Okay, now that we've got a couple of ways to access the database on a background thread, 41 00:04:18,800 --> 00:04:21,760 we'll clean up our add/edit fragment. 42 00:04:21,760 --> 00:04:24,020 It shouldn't be saving the data. 43 00:04:24,020 --> 00:04:27,100 That function should be moved into the ViewModel. 44 00:04:27,100 --> 00:04:32,160 The database access to save the data should be performed on a background thread as well. 45 00:04:32,160 --> 00:04:35,400 We'll make both those changes in the next video. 46 00:04:35,400 --> 00:04:36,860 Look forward to seeing you there.