Coroutines with Firebase Firestore - Kotlin Coroutines

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys and welcome back to your new video this video will be about using pure routines in combination with firebase firestore if you use firestone a real project it can often happen that you get in the so called callback hell that happens if you have several network operations that all depend on each other and always use callbacks and that is basically how fast are worked in the past so let's say you want to construct a chat between two users first you need to get the data from user one then you need to get the data from user two and then you need to get the messages these users wrote each other to finally be able to construct a chat object in the past this was usually handled with Kovac functions so the requests run asynchronously and notify us with the data when it's available so you can think of it for example as a function get user one that is a lambda function it gives us user 1 here and because all of that data we want to get here relies on each other so we need to get user 1 user 2 and the messages to finally construct a chat object then that means we also need to call get user 2 inside of this get user 1 function because only if we got user 1 we want to call get user 2 because then we want to get the second user and in here we finally want to be able to get the messages between those two users so we call a function get messages and here we can finally construct or a chat object and as you can see that is super ugly code and that is also exactly what is considered the callback hell because you have so many callbacks nested into each other and there's actually no way out but luckily since we have crew routines there is a way out and that is using pure routines because they can just pause and resume they don't rely on callbacks so we can just make Network call 1 pause until the data is available make Network call 2 pause until the data is available again and so on so we can get all the data without indenting our code but let's actually see how we can use curtains with firestore on a real example for that we need to add an additional dependency which our pasted in the description of course so I will just paste it in my built-up Gradle file here and I also assume that you set up fire store already because this tutorial is not about setting that up if you don't know how to do that then you shouldn't watch a video about using routines and faster and you should rather learn how to set it up first so I will just paste the dependency here that is the skirtings Play services dependency and what I also want to change is I want to use the KTX dependency of faster so we can just append a - KTX here that is just basically the Kotlin version of file store and it's optimized for Kotlin so if you have done that click on sync now and go back into main activity and just to give you a little insights here's my fire store database I just created collection curtains and a document tutorial in that which is currently empty and I'll just show you how it can set data to that document while losing curtains and after what we will get that data and display it on a textview in our app so the textview is actually in my activity main I already created that it is called TV data and initially says it didn't get any data yet so make sure to set that up and jump back into main activity first of all let's get the document reference well tutorial document and I will set it equal to firebase dot fire store dot and we have to import firebase.com here you have to insert your own collection name of course mine is guru teens and get the document after rods with the name tutorial and let's actually move this into a separate line here for readability then I will quickly create a data class person up here which will just be the data that I will paste in my database in my document I will give the name which is a string of course and set it to an empty string initially and make sure to really give these arguments default values because otherwise they won't with my store and I will give it an H which is an integer and set it to minus one then we can go down and create an instance of that person class I'll just call it Peter which is person of course the name is Peter and he is 25 years old then we can start a routine now I will just use global scope here Globes got launched and I will launch this in the i/o dispatch of course because we are using an i/o operation here and when we now want to add data to our file store document then the only thing we need to do is we need to take our tutorial document or document reference and call that set after that and then we can pass a class here so basically any object that should be saved in that document in our case that is just our Peter and that is it but if we leave it like that it won't actually make use of our accrue routine instead what we need to do is we need to call that a wait afterwards oops dot oh wait and if we press on a wait and press ctrl + Q you can see that is actually a suspend function so that will block or accrue teen until the data is available on this case until the data is successfully set to our document and in general every time a firebase function returns a task object like this set function if we press ctrl Q on that you can see this returns a task then we can call dot await on that and basically execute this task in our crew routine so as you can see it's super easy to use care routines with file store let's also get the data that we just set to our document by writing tutorial document dot get this time and as you can see this get function also returns a task so we can call this a weight function on that let's do that dot weight and we will also want to convert this to an object so we are actually able to get the data in form of cotton object so we call that to object afterwards and now we have to provide a class so Carly knows how to interpret that data and that is exactly our person class that should be the output of that function and this time we of course want to save that in a variable so well person is equal to this piece of code and to actually set that person data now to our text view if you remember we need to switch the context of our routine because we are currently in the IO dispatcher and we are only allowed to change UI in the main dispatcher so let's write with context dispatch just up main and in here we just call TV data cut text is equal to person that to string and let's also actually add a little bit delay here like three seconds is fine I guess so we just see what's going on because those two lines will will be executed immediately we won't even notice the time difference so let's run our app and try it out you can see didn't get any data yet and after three seconds it got the person data from our fire store and also if I take a look at my file store there is our Peter with age 25 and what we prevent it right here is basically that we have and on success listener here on success task let's choose this and inside here we get the person again without a wait have an on success task again so that callback hell you know what I mean it's just something that you can very easily prevent with coroutines and that's why they are so popular and cool right now and also as you know from the last video routines are actually lifecycle aware so we also don't need to worry about asynchronously executed file store calls that still want the event when the activity is stopped normally we would have to write some logic to prevent that but with crew routines we can just use the lifecycle scope to prevent that so it's really cool so I hope this video gave you a good overview of routines for firestore if it did then please let me know in the comments and also if there's anything I can improve on let me know that would be really helpful for me have a good day see you in the next video bye bye
Info
Channel: Philipp Lackner
Views: 12,926
Rating: undefined out of 5
Keywords: kotlin, coroutine, asynchronously, network, networking, request, tutorial, android studio, android, learning, beginner, intermediate, firestore, firebase, auth, thread, programming
Id: DxvRcomIAQg
Channel Id: undefined
Length: 9min 19sec (559 seconds)
Published: Tue Apr 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.