From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Define and run a simple coroutine
From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Define and run a simple coroutine
- [Instructor] One of the great advantages of moving to Kotlin from Java is that you get to use Coroutines to manage long running tasks and multi threading. Instead of using call backs or sending complex messages between threads, Coroutines can include code that switches back and forth, as needed, between the main thread and background threads. The resulting code is more readable and easier to maintain because it looks just like synchronous code. To get started with Coroutines, add a couple of dependencies to your apps build.gradle file. I'll need two dependencies, and they'll always use the same version so I'll define a variable. You can name this variable anything you like, I'll call it coroutines_version, and I'll set it to to a value of 1.3.2, because as of the time of this recording that was the most recent version of the Coroutines API. Next, I'll add an implementation. I'll set this string inside a pair of double…
Contents
-
-
-
-
-
(Locked)
Define and run a simple coroutine4m 47s
-
(Locked)
Switch between foreground and background threads4m 23s
-
(Locked)
Run coroutines in a ViewModel class5m 23s
-
(Locked)
Cancel coroutines with a job reference3m 40s
-
(Locked)
Challenge: Run multiple coroutines in a ViewModel2m 28s
-
(Locked)
Solution: Run multiple coroutines in a ViewModel3m 34s
-
(Locked)
-
-
-