In this video, learn how to use the launch function to start a coroutine. Review the input to the function as well as how to simulate a delay.
- (Instructor) Before we start working with coroutines in our code directly. Let's go over working with the launch function, by means of a few text examples. So we're here in the coroutines launch test class, and we're going to start by adding some code to the start with launch no sleep method. Now the most basic way to start a coroutine is with the launch function. So we're going to start here on line nine, and we're just going to add a few markers for ourselves, by means of print line statements. So we're going to do starting, duplicate this, and then we're going to put stopping.
Okay, and in between is where we'll handle our coroutine information. So first with the call to launch, this is how we're going to initiate our coroutine. Now let's just take a quick peek at the definition of this particular function. You'll see that it takes in the coroutine context, which is related to the threads that you want your work to happen on. Next, the coroutine start enum, which gives you some control over when the coroutine is executed.
And then finally there on line 54, you see that it takes in a suspending lambda, and this is where you're going to put the work that you want your coroutine to do. So let's switch back over to our test, and inside of our launch function, we're going to do all over our work on the common pool. So this is just a general pool of threads. And then the work that we want to do, is we're going to print out inside. Okay, so let's go ahead and run this test.
And if we look at our output, we get starting, stopping, and then inside. Well this is because we're doing the work of our coroutine on another thread. So although the output came later, we still have time to complete it. Now let's go ahead and update this test, so that we have a slight delay inside of our coroutine before we print our message. And for that, we're going to use the delay function. And this is going to accept some time in milliseconds. So let's do a thousand milliseconds.
Now I want you to notice what happened inside of our IDE here in the gutter. It added this little icon. So we hover over it, and it tells us that this is a suspend function call. Now this means that it can cause our main launch coroutine to be put aside before continuing with its work of printing inside, there on line 16. So let's run this method again. And this time, our output, we only get starting and stopping Now that's because we're done with the test before the coroutine had the chance to complete.
Now, let's see our coroutine actually complete. For this, we're going to move on to our next method, start with launch sleep. So let's go ahead and copy this same code from here. And then we're going to paste this starting down on line 24 in our new test method. But what we're going to do differently, is after our launch coroutine, we're going to add some sleep in there. So we're just going to use standard thread sleep method. And less sleep for a second and a half.
By placing it outside of our coroutine, this is going to give it a chance to complete. So let's go ahead and run this test. And it gives us just what we were hoping for. Starting, inside, and then stopping. This helps us to see that coroutines are not blocking the main thread, and this makes them really useful for getting asynchronize work done on android.
- 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