Database and Room TypeConverter - MVVM News App

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to me video in this video we will set up our actual database class for room and also what we will set up is a type of order for room but you will understand what that is when we actually do that so let's start by going into our DB package right click and create a new cutland class I will call this article database article database it's like class of course and press ENTER and this will be an abstract class database classes for room always need to be abstract and we need to add an annotation for that class which is at database to tell room that this is our database class then the first property in the first parameter of that database annotation will be our list of entities we only have one single entity so one single table in our database which is the article table so we need to pass that as an array and we simply pass article double colon class and import article here and we should also define a version for our database I'll set it to 1 initially the version is used to update our database later on so let's say we want to make some changes at some point then we need to update that version so that room knows that we made some updates to our database and that we should migrate our own old database to our new database then we need to let this class inherit from room database then we can go inside of this class and here you need to create an instance of our article there or at least not not an instance we need to create a function that returns an article Dow an abstract function I'll call it get article Dow and this returns an article Dow and that is everything we need to do for this function that is an abstract function so we don't need to implement it and the implementation of that will happen behind the scenes room will do that for us then I will create a companion object to be able to create our actual database and first we want to create an instance of that article database it will be a private var instance and that is an instance of article database which is nullable and we set it to null initially we need to annotate that instance with ad volatile that means that other threats can immediately see when a threat changes this instance so that is really useful and we also want to create a lock variable private Val lock which is just of child any we'll use that to synchronize setting that instance so that we really make sure that there is only a single instance of our database at once but you will see what I mean with that when we write the next function which is an operator function and you need to call this invoke that is called whenever we create an instance of our database so whenever we write something like article database and call the constructor on that data base then also this invoke function will be called so basically when we initialize or instantiate that object and this function will take a context and we will set it to our instance and if the instance is null in that case we want to start a synchronized block with our lock object so that means that everything that happens inside of this block of code here can't be accessed by other threads at the same time so we really make sure that we don't set that there is not another threat that sets this instance to something while we already said it and in this synchronous block we want to return our instance again so we make that null check again and if it is still null then we want to call a function that we create afterwards which is create database which will take our context and we call dot also on that which sets our current instance to it and let's actually create that created a great database function here which is a private function create database which takes a context and we will set it to room dot database builder and in here we need to pass the application context we can get that from context dot application context and we need to refer to our database class so to this current list which is article database double colon class stat Java and we need to choose a name for our database which I choose is article underscore DB dot DB and don't forget co dot build afterwards to actually create our database and I know that this is very confusing for people who have never worked with room before everything that happens here is we create an instance of our database which will be our singleton so we will only have a single instance of that database and in our invoke function here whenever we create that instance of that article database class then we return the current instance and if it is null we will set that instance in the synchronize block and we check again if it is not null but if it is null we call our create database function and also set our instance to the result of our create database function and this database class will then be used or that instance of that database class will then be used to access our article Dao which is used to access our actual database functions and the last thing that we need to do to get our database running is what I already spotted a little bit in the beginning we need to add a type converter to our database so if we take a look in our article class then you can see that we have integer string string and so on and here we have a source that is our very own source class that you can see here that only consists of an ID and a name that will be the source of a specific article so basically from which publisher that article came and the problem now is that room can only handle primitive data types and strings so very basic data types but not our custom classes and we need to create what is called a type converter to tell room it should interpret that source class and to convert that source class into a string for example and on the other hand if we have a string you need to tell room how it should create that source class all of that string and for that we create that type convert a class let's go to our DB package create a new Kotlin class and our count I'll call it converters make sure to select class and that will be a very simple class we have a function to define what happens if we want to convert to string from a source so we call it from source and pass source as a parameter that will be the source that we will convert it to a string so that needs to return a string here and to tell room that this is a converter function we need to annotate that as type converter and in this example I really only care about the name of that source we don't want to know about that idea that's not really important for us so I will just return the name of our source here so whenever we get a source then we tell room that it should convert that source to a string by just taking this the name of the source and then we will have another function which is also a type converter which will be a function to source so whenever we have a string then we want to convert that string to our source class and that will just return a new source and just pass our name twice that is fine for our needs here and that is really everything we need to do for this class the very last thing we need to do is to actually tell our database that we want to add these type converters to that so let's go to our database class article database and above that class creation we simply want to add an annotation at type converters and in the parentheses we want to pass the class of RF converters so converters double colon class and by the way let's also create a new package for our models so they are not in our route package that is not a really good package structure so I create a new pack call it models and then I will move all of our models in there so article news response and source and that is it for this video we finally set up our whole room database if this was helpful for you to understand all this then please let me know in the comments and also if there's anything I can improve on please let me know that too that would be really helpful for me have a good day see you in the next video bye bye [Music] [Music]
Info
Channel: Philipp Lackner
Views: 14,610
Rating: undefined out of 5
Keywords: tutorial, android, development, learning, programming, programmer, kotlin, beginner, coroutines, coroutine, retrofit, navigation components, room, database, android studio, typeconverter, typeconverters, networking, network, api
Id: 3LeJR-k1yYs
Channel Id: undefined
Length: 9min 43sec (583 seconds)
Published: Sun Apr 26 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.