All right, so let's get back to our project in which we are now going to take care of the repository.
So you see the room database, including Dow and entity is done now, at least prepared.
Now we're going to adjust accordingly once we need them.
But now let's take care of the repository, which is the single source of truth for all data, a clean
API for UI to communicate with.
OK, so you I can then communicate via the view model with the repository, which then communicates
with the database.
So that's basically the structure here.
So let's set up our repository.
And by the way, you can check out how the repository works here.
So about a repository class abstracts access to multiple data sources.
The repository is not part of the architecture component libraries, but is a suggested best practice
for code separation and architecture.
So why would you use it?
Well, it manages queries and allows you to use multiple back ends.
And the most common example, the repository implements a logic for deciding whether to fetch data from
a network or use results cached in a local database.
So that's pretty cool.
And we are going to, in fact, use both of the features.
We're going to get data from a network as well as use down.
So let's look at the implementation.
You can see we take the name of our data model and then repository, at least in our case, it will
be then the dish repository.
Then we need to pass our Daou into the constructor, as you see here.
And we're going to use next is this suspend here.
So this is a worker thread and the suspend function is part of the routine of the routine structure.
So the cool thing is it can be called and the UI thread, even though it works in the background.
But this suspend is just going to say, OK, please execute me real quick and then do your other stuff
that the UI needs to finish like that.
It basically needs to call every time reacting to the user input and so forth.
And once you are done with whatever you're doing, let me know.
And that's what this suspend function will do for us, which is pretty cool because we can just use
the same kind of structure that we would have used in normal coding examples.
But we have something that runs in the background and does it very efficiently.
So what is to consider is the fact that the Dow is past the repository constructor as opposed to the
whole database because it doesn't need to have access to the whole database.
It really just needs to have access to our Dow, which then contains the right methods for our database.
And, yeah, that's basically the idea.
Now, there is one more thing to consider, and that is this part here.
So I said that suspend functions can be called in the main, Fred, but room executes suspend queries
of the main threats are not on the mainframe part of the mainframe.
OK, now let's get back to our project and actually set up our repository.
So this will also be part of our model and database.
So here.
Right, click new and this one will be a class.
And I'm going to call this five days repository.
All right, so now what is going on in this class?
Well, we saw we need to pass the Dow to it.
So private vol Fev, the Dow will be our and should maybe be uncapitalized our fetish.
Now, some of it feels like by and then it feels like Southeast Asian meal or something like that.
So, yeah, now we need to go ahead and take care of our suspend function.
So I'm going to call the insert dish data, which will need our favorite dish, which is of type fav
dish.
OK, so this is going to be the type of data that we're going to pass here and now we can use our first
digital.
In order to call our insert fetish details with the fetish that was passed to our.
Suspend function, insert first dish.
So now we need to make this a worker thread, which denotes the annotated method should only be called
on a worker thread.
If the annotated element is a class, then all methods in the class should be called on a worker thread.
Okay.
So in our case.
It is not going to be the whole class, but only this particular method will be called on that worker
thread and not on the main UI thread, so to speak.
All right, so now looking at this architecture that we're using, we are done with the room database,
we prepared our repository and now the next step will be to take care of the view model.
So let's do that in the next video.