This course will be retired on June 1, 2025.
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 verify that saving a pizza in the database works like we expect it should!
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 just tried running the app and
found out it won't even compile.
0:00
Let's look at the error and
see what it says.
0:04
Cannot figure out how to save
this field into database.
0:07
You can consider adding
a TypeConverter for it.
0:11
And it looks like it's talking
about a date called creationDate.
0:14
If we click on this error,
We're brought into this strange file.
0:19
Though actually,
0:27
this is just what our Pizza class looks
like once Room's finished with it.
0:28
And we can see that the error, again,
is with the creationDate field.
0:32
In Room, we're only allowed to store
primitive data, like ints, longs, and
0:37
strings.
0:42
If you want to store something more
complicated, then you need to break it
0:43
down into primitives before
you store it in the database.
0:46
This is where a TypeConverter comes in.
0:50
A TypeConverter is a class that
defines how to break an object down
0:52
into primitives and
how to build it back up.
0:56
By using TypeConverters, we can store
pretty much anything in our database.
0:59
As you probably guessed,
we'll need to create a TypeConverter
1:04
to tell our database what
to do with data objects.
1:07
A common way to represent a date is
to use the number of milliseconds
1:11
since January 1, 1970, and
store that number as a long.
1:15
Let's close this file and, actually,
a lot of these other files.
1:21
And then, inside our data package,
let's create a new class,
1:27
Named DateConverter.
1:35
Inside this class, let's write one
function to turn a date into a long and
1:37
another to turn a long into a date.
1:42
So fun dateToLong, which takes in
1:46
a date as the parameter and returns Long.
1:51
And let's use some Kotlin shorthand and
just use an equal sign to say
1:58
that this function should return
date from the parameter .time,
2:03
which gives us the Long we're looking for.
2:08
Moving on to the next function,
let's type fun longToDate.
2:13
Make it take in a Long and return a Date.
2:21
And then let's use the long parameter to
make this return a new Date instance.
2:26
Again, using the equals sign syntax.
2:30
So =, a new Date object, where we
pass them along to the constructor.
2:35
Awesome, all that's left is to
annotate each of these functions as
2:40
TypeConverters and then attach our
date converter to our database.
2:44
So let's add the @TypeConverter
annotation above our two functions.
2:48
@TypeConverter and @TypeConverter.
2:53
And then, let's open PizzaDatabase.
2:58
And let's add a line after the Database
annotation, but before the class, and
3:03
add an annotation for
TypeConverters, with an s.
3:07
Then lets add parentheses, and we just
need to pass in our DateConverter class.
3:12
So DateConverter::class.
3:17
Perfect, our TypeConverter
should be all set up.
3:21
Let's head back to our PizzaTest class And
run the test.
3:24
Also notice that our test is the preferred
run option at the top of Android Studio.
3:34
So we can also run it by using
the play button up here.
3:39
And we passed our test.
3:46
Great job getting that all set up.
3:48
In fact, let's run this one
more time as a victory lap.
3:50
Well, that's not good.
3:55
Looks like we failed a unique
constraint on Pizza.id.
3:58
When we ran it the first time,
we inserted a Pizza with an id of 0.
4:03
Now, since that Pizza's still there,
4:08
it won't let us insert another
pizza with the same id.
4:10
When we're writing tests,
4:14
we want to make sure that we're always
starting the test from a clean slate.
4:16
To do this in Room, we can use the
clearAllTables function from our database,
4:20
which removes any data from our tables.
4:26
Let's add a line below where
we declare our database and
4:29
add a call to db.clearAllTables,
then let's run the test again.
4:33
And awesome, we're back to passing.
4:43
Now that we've tested the ability
to save a pizza, in the next video,
4:46
we'll test that we're able to save
that pizza's toppings as well.
4:50
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