Anko provides a couple of functions that you can use to make working with coroutines on Android easier. In this video, take a look at the bg() function and how you can use it to handle background tasks.
- [Instructor] Anko provides us with a couple of functions that we can use to make working with coroutines on android easier. Let's take a look at the most useful one. First, we need to make sure our built out gradle file has the anko coroutine dependency, just like you see here on line 39. So now we can go ahead and update our activity to take advantage of our function BG. So let's switch over there now, and here on line 27, this is where we grab our list of planets and then we populate it in our adaptor.
We're just going to comment this out for now. And we're going to reference a new function, which we're going to call get planets time consuming. And this is basically just to simulate that we're executing a long running network call. So let's go ahead and use alt enter to create this function. And we'll delete the template contents. Now the first thing we want to do is to start our coroutine using the launch function.
And for our coroutine context, we're going to provide the UI coroutine context because this executes on androids main UI thread. Okay, so we're off to a good start. Now next, let's add a log message here on line 35. So that we can actually see which the rate we're executing on. And once again, this is from the anko info package. And we're just going to put starting launch on, and then let's grab our threads name.
And we'll use thread dot current thread dot name. Okay, perfect. So the first thing we're going to use from the anko coroutine artifact is the BG function. And that just stands for background because this allows us to execute our code on a background thread and then it's going to return the result as a deferred of some type. So it's basically just a shortcut for the async function. So right underneath here on line 38, we're going to start our BG function.
And it's going to take in a lambda, and let's go ahead and grab our info line from 37, and paste it inside here on line 40, so that we can see which the rate we're going to start on for the BG function as well. Okay. Now since I said we want us to simulate that this is taking a long time, we're going to make a call to thread dot sleep. And that's just sleep for one second. Okay. And then finally we want to return our list of planets So it's going to be from the same thing that we have before.
Our planets data provider. We're going to return our list. And since we're returning something, that we want to grab that value from the BG function here on line 39. So we're going to set this equal to planets, because that's what we're going to get back. A list of planets, but it's going to be a deferred list of planets. Then let's close all of those angle brackets up, and now we're good to go.
So the final step is that we want to update our adaptor, just like we did before up there on line 33. So at the very end of our launch coroutine, we're going to update the planet list adaptor. Create a new adaptor, but what we pass into it is what differs. Because we want the result of the planets from the background call. So we're going to pass in planets dot await.
So if your call await is that suspending function that lets us get our results out of a deferred. So now let's go ahead and run our application again, so we can see the list of planets populate, but in a delayed manner. Let's switch over to our emulator. You see the delay and then the planets came in. So let's look at the logs because the most important thing here is which thread did everything happen on.
So if we come back over to android studio, and we open up our logs. Right here we see that the launch coroutine happened on the main thread, but the BG coroutine happened on the BG dash one thread which is a background thread. Perfect. So this is how we've been able to take advantage of this background function that's available from the anko coroutine package.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections