Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll use a LiveData to update the UI whenever something changes in the database!
Code
private var pizzas: LiveData>? = null
fun getPizzas(): LiveData> {
if (pizzas == null) {
pizzas = db.pizzaDao().getAll()
}
return pizzas!!
}
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've just added functionality
to our save and delete buttons.
0:00
But that doesn't do us much good if
main activity won't show the pizzas.
0:04
Just like how we created a view model for
our creator activity,
0:08
we should also have a viewModel for
main activity.
0:12
This viewModel will store which pizzas
should be displayed in main activity,
0:15
let's create a new class named
MainViewModel, inside of our main package.
0:19
Then let's make it extend from viewModel,
And
0:32
use Alt + Enter to add
the call to the constructor.
0:38
Inside this class we'll want to
maintain a list of pizzas, and
0:42
we'll want that list of pizzas to
be based on data in our database.
0:46
By using something called live data,
0:50
room gives us a way to tie
together our UI and our database.
0:52
So if something changes in our database
that change will be automatically
0:57
reflected in the UI without
us having to do anything.
1:02
Inside the class, let's copy and paste
in the code from the teacher's notes and
1:05
then walk through it.
1:09
Use Alt + Enter to import the pizza
class and the liveData class.
1:13
We start by creating a private
variable named pizzas,
1:18
which is a liveData containing a list
of pizzas, and we set it equal to null.
1:22
When we call the getPizzas function,
if our pizzas object is null,
1:28
then we populate it with
pizzas from the data base.
1:34
Otherwise, we'll just
return our pizzas object.
1:39
This way, we're only loading from
the data base when we need to, we should
1:43
also fix this error by making pizzaDao
get all function return alive data.
1:48
Let's use Cmd or
Ctrl + B to jump to the declaration, and
1:55
then add live data around
the list of pizzas.
1:59
Perfect, and that's really all you
have to do to use a live data.
2:11
Okay, now to make a use of our live data
object, let's head over to MainActivity,
2:17
And add some space at
the bottom of onCreate.
2:24
Then to retrieve our live data, we'll
first need to retrieve our view model,
2:29
then call the getAll method.
2:34
To retrieve the view model, let's do
the same thing we did in creator activity.
2:36
Start by calling viewModelProviders
with a capital V,
2:41
.of and passing in this for our activity.
2:48
Then on the next line, let's add .get and
2:53
pass in the Java class of our ViewModel,
2:57
MainViewModel::class.java.
3:02
Now that we've got the viewModel on the
next line, let's add a call to getPizzas,
3:06
To retrieve our live data
containing a list of pizzas.
3:14
Once we've got the live data,
3:18
we can get notified when anything
changes by adding an observer.
3:20
On the next line, let's call
the .observe function on our live data.
3:25
Then we need to pass in a lifecycle
owner and an observer object.
3:32
Luckily, our activity is a lifecycle
owner, so for the first parameter,
3:37
let's just pass in this.
3:41
Then for the observer parameter,
let's pass in a new observer
3:44
and use Enter to select
the option with the brackets.
3:49
Now whenever the underline
pizza data changes,
3:54
it'll call the code
between these brackets,
3:58
inside it will be a list of
pizzas that might be null.
4:02
So first let's make sure that
the list of pizzas is not null.
4:07
So if it is != null, and
so long as it isn't null,
4:11
we'll want to update our adapter
to use the new pizza list.
4:17
An easy way to do this is to just clear
the adaptor and then repopulate it.
4:25
Inside the if statement let's start
by clearing our adaptors list.
4:30
So my adaptor.list.clear.
4:35
Then let's add our new pizza
list to the adaptor's list.
4:42
MyAdapter.list.addAll, And
we'll pass an it.
4:46
And finally let's notify the adapter
that the data has been changed.
4:56
MyAdapter.norifydatasetchanged, great.
5:01
Now if we run the app we should be
able to save and delete pizzas, and
5:06
those pizzas should show
up in main activity.
5:10
Or we'll get an error.
5:17
It looks like Room can't
verify the data integrity.
5:22
It looks like we've changed schema but
forgot to update the version number.
5:26
And it suggests we can fix this
by increasing the version number.
5:33
Okay, let's head in to
our PizzaDatabase class,
5:38
And change the version from 1 to 2.
5:47
Then let's run the app again,
And we got another error.
5:53
This time it says a migration from
1 to 2 was required but not found.
6:01
Please provide the necessary
migration path.
6:07
In Room whenever you change
something about your database,
6:11
you need to tell Android how to handle
that change by specifying a migration.
6:15
If you'd like to learn
more about migrations,
6:21
check out the teacher's notes below.
6:23
But for us rather than handle a migration,
when we make a change,
6:26
we'd like the database to
just start from scratch.
6:30
To do this, In the app class,
6:34
Where we declare our database,
6:41
let's move the call to .build
down a couple of lines and
6:44
in the middle add a call to
.fallbackToDestructiveMigration.
6:49
Then if we run the app one more time,
we should be good to go.
6:55
So let's try creating a pizza.
7:05
Let's add some toppings, Give it a name,
7:08
Make sure it stays across rotation,
all right.
7:19
And let's save it, and there it is our
pizza is being shown in main activity.
7:24
And if we click on it, well it looks like
we haven't handled that part quite yet.
7:32
In the next video, we'll see what's
going on when we try to select a pizza.
7:37
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up