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.
Switch between foreground and background threads
From the course: Concurrent Programming with Android: Threads, Workers, and Kotlin Coroutines
Switch between foreground and background threads
- [Narrator] When you launch a Coroutine the Dispatcher determines whether you're working in the UI thread or in a background thread. But it's possible to switch back and forth between threads in the middle of the Coroutine. Without Coroutines, you'd need to create callbacks or send messages to communicate between the threads. But in a Coroutine, your code can be sequential and all in one place. In this branch of the repository, I've added a constant at the top of the class. This is a URL where I've placed a file. I'll copy that URL to the clipboard, then I'll go to a browser window and paste it in and show that this is just a text file with some placeholder text. In order to download that from the web my app needs the appropriate permission. So I'll go to the manifest file and I'll add a user's permission element and I'll add the permission for the internet. This is a normal permission so it will be granted…
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)
-
-
-