Async and Await - Kotlin Coroutines

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to new video this video will be about the async and weight functions for curry teens so if we have several suspend functions and execute them both in the crew routine then they are sequential by default which means the first function will be executed first and when it is finished the second one will be executed however if we want to do two Network calls for example then we actually don't want to make them one after another we rather want them to be executed at the same time so for demonstration let's actually create two system functions on network call one that return a string and simply delay this function for three seconds and then return and so on then we can copy that function and paste it below just for the second Network call at work of two and return answer two so if we now launch a curtain here close called launch now I will launch it in the IO dispatcher dispatches dot IO and inside of this protein we write something like well and so on is equal to network call one duplicate that line and do the same for our second answer and then print a log message block D pass our check and print answer one is and then pass our value of answer one duplicate that line with control D do the same for answer to what I want to show you that now is that we execute that Network call one function first and then we execute network called two and both functions delay the current routine for three seconds and what you will see is that they that if we execute both one of the another it will actually take 6 seconds until this code gets executed but in practice if we have two different network calls like we have here then we could just execute them as in Kronus li so at the same time so that we can go on with this code after all three seconds and to show you that time difference cotton has a very cool function with which we can easily measure the time a piece of code needs for execution and that function is called measure chime Milly's and this will just have this code block and it will measure the time of the code we will put inside of this block and then we can simply print a log after the ad that prints how long this piece of code needed to execute so we can simply write requests choke and then pass our chime milliseconds so now let's run our app and take a look and look at you see the network calls are running and when they are done it will print both answers and the requests took about six seconds so that is definitely not what we want so how could we solve this problem well what we could do is we could just start a new crew team for each function we execute and set the answers accordingly in those crew routines so let's try that let's actually delete those two variables here and declare the new as a var var answer 1 which is a nullable string because we will set it in routine set it to null initial leaf do the same for answer 2 then we can launch a nuke routine for each answer and set answer 1 to network call 1 do the same for answer to network call - and what we also have to do is we have to wait for those two crew routines to finish so we actually have to save them in a job so Val job 1 and Val job - because if we wouldn't wait for that then we would launch those coroutines but they take 3 seconds to finish and our code would continue immediately with this code and it would just print answer 1 is null and answer - is not so we need to call job one or join and job - to join and if we now execute this code take a look a locket you can see now the requests took only about three seconds so that is definitely better but the approach how we do it is actually very terrible so you definitely don't want to do it like I did here that was a very bad practice instead what we can do is we can use async it is very similar to launch it will also start a new crew team but it won't return a job like the launch function instead it will return a deferred and this deferred can be used to get the result of the calculation or of the network call so we can write something like well answer one is equal to a sink which will just start a new crew routine here and inside a sink we just put our function we want to execute which is network old one and the last line of this async function will be returned so the result of that Network call function and we can duplicate that line answer to and network call to and because those answers here are no jobs anymore which we would get from a launch block here but we have an async block here so if we take a look press ctrl Q you can see it is a deferred of a string and the string is actually the return value of that async block so whenever we want to do something asynchronously and get a result of that we should use that async instead of launch and to actually get the result we cannot just pass answer because answer is a deferred like I showed you instead we have to call answer one dot a weight which will just block our current crew routine so this one until the answer one is available and the same for answer two so if we now rerun our app take a look and lock it again then you will see that our requests will take about three seconds again so it's the exact same as we did before but in a much better rate so you should always use async if you want to use a crew T in that returns some kind of a result and by the way you can also use this async instead of launch for the global scope so we could just pass a Singh here and that would work fine too but in this time it doesn't make any sense because our routine here does not return anything so we should replace it with launch again if you found this video helpful then please leave a comment and a like and also if there is anything you didn't like about this video please leave some constructive feedback that would be really helpful for me to improve on my content have a good day see in the next video bye bye
Info
Channel: Philipp Lackner
Views: 18,194
Rating: 4.9928699 out of 5
Keywords: kotlin, coroutine, thread, async, await, asynchronous, delay, suspend, function, programming, tutorial, coding, development, android, android studio, native, ktx
Id: t-3TOke8tq8
Channel Id: undefined
Length: 7min 16sec (436 seconds)
Published: Fri Apr 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.