Coroutines with Retrofit - Kotlin Coroutines

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to your new video in this video I just want to quickly show you how we can use retrofit in combination with goroutines and as you can see I already created a very basic retrofit setup here I will use this Jason placeholder URL which is just an open API you can try round with and I will get some example comments from that side so I created that my API interface that gets the comments from this website with a sketch Commons function and I create a data class for a comment and that's basically it don't forget to include the internet permission but yeah normally if we do a retrofit request we did it like this so we called our API then the function of that API that gets your data and called n:q afterwards that in queue function will start that request in a separate thread and then use that callback that we pass here afterwards to notify us when the response is complete or when the response failed and if the response was successful then I just print out all the comments that are included in that response so let's quickly try that out so you can see in my lock ad there you go here all the comments it's working perfectly fine but we start a whole nother threat with this in queue function and as you probably already know that is not very efficient because we have cout routines which are way more efficient so it would be much smarter to not start a whole nother threat for that request and instead execute that request inside of a crew team so let's actually do that and remove that block of code here and instead start a curtain in global scope global scoped of launched and started in dispatches dot IO of course because that is an i/o operation and here we just write well comments which is the result of that request and set it equal to MPI dot get comments dot await that's it then we can just use the exact same followed afterwards for each comment in comments and then just lock the result so if you now run our app again and take a look and lock it you see it is working exactly the same as before it's printing out our comments here and that is of course much much simpler and much cleaner code than what we had before and maybe you're asking yourself what if we want to still want to get the response object of that retrofit request because now that a rate function gives us directly the list of comments but in case an error occurs at our request then we cannot really handle it and that is actually also very easy to solve we instead of a rate we just call a way to response and then our comments is not a list of comments anymore instead it's a response of list of comments so we could call this response in this case then we could check if that response was successful and in that case execute that for loop with our response response starts buddy and here of course assume that this is not now so that is one way of using retrofit with curtains another way which I actually prefer is that we just go into our API class in our interface and make these functions suspend functions and also make sure to not return a call object in that case because now it can directly return a response object and if we now go back into our main activity you can see that we cannot use a wait response anymore and we don't need to use it anymore because we can directly call API to get comments and because that is a suspend function it will only continue afterwards if the comments are actually retrieved from the API so yeah I think you get why you should always use retrofit in combination with coroutine on the one hand it's much less code and on the other hand it's just much more efficient to start a crew routine than to start a whole new thread for each request you make so yeah if this video helped you to understand how to actually use routines in a real Network call example then please let me know in the comments and also if there is anything I can improve on please leave me some feedback below that would be really helpful for me have a good day see you next video bye bye
Info
Channel: Philipp Lackner
Views: 19,480
Rating: 4.9541445 out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, coroutine, async, suspend, function, concurrency, network, retrofit, remote, api
Id: S-10lLA0nbk
Channel Id: undefined
Length: 4min 55sec (295 seconds)
Published: Thu Apr 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.