1 00:00:00,000 --> 00:00:00,869 [MUSIC] 2 00:00:00,869 --> 00:00:07,110 We've just finishing building our database. 3 00:00:07,110 --> 00:00:10,070 Now, to finish up the app, we just need to connect it to the UI. 4 00:00:10,070 --> 00:00:14,960 We'll start by updating the save button and creator activity. 5 00:00:14,960 --> 00:00:19,460 And by the end, we'll have the UI updating itself whenever anything changes. 6 00:00:21,050 --> 00:00:25,000 The first step to using our database is to retrieve an instance of it. 7 00:00:25,000 --> 00:00:28,130 Rather than retrieving our database all over the place, 8 00:00:28,130 --> 00:00:32,140 let's just get it done once in our application's onCreate method, and 9 00:00:32,140 --> 00:00:34,620 then make it available to the rest of our project 10 00:00:34,620 --> 00:00:40,120 by declaring that variable outside of the class to scope it to the entire project. 11 00:00:40,120 --> 00:00:42,150 Let's go ahead and close the files we have open. 12 00:00:44,090 --> 00:00:46,160 And then head over to the app file. 13 00:00:48,360 --> 00:00:52,550 And below our toppings array let's create a new lateinit var 14 00:00:53,660 --> 00:00:57,380 named db and give it a type of pizza database. 15 00:00:59,780 --> 00:01:04,110 Then down in onCreate let's add a line at the top and 16 00:01:04,110 --> 00:01:09,400 set db equal to a new instance of our pizza database. 17 00:01:09,400 --> 00:01:14,400 So Room.databaseBuilder, 18 00:01:14,400 --> 00:01:18,300 we'll pass in the context, which is just applicationContext. 19 00:01:19,890 --> 00:01:24,530 Then the database class PizzaDatabaseclass.java. 20 00:01:24,530 --> 00:01:28,846 The name of the database which is 21 00:01:28,846 --> 00:01:34,471 "PizzaDatabase").build(). 22 00:01:34,471 --> 00:01:35,480 Great. 23 00:01:35,480 --> 00:01:39,310 Now whenever we want to access our database we'll just type, db, 24 00:01:39,310 --> 00:01:41,480 and be done with it. 25 00:01:41,480 --> 00:01:44,220 Now another thing we'll want to do in this onCreate method 26 00:01:44,220 --> 00:01:46,870 is add our toppings to the database. 27 00:01:46,870 --> 00:01:49,580 Since we've already got a loop going over the toppings list, 28 00:01:51,150 --> 00:01:54,280 let's just insert each of these toppings to the database. 29 00:01:57,070 --> 00:02:03,220 So let's just add db.toppingDao.insert. 30 00:02:03,220 --> 00:02:04,260 And pass in the topping. 31 00:02:06,160 --> 00:02:09,550 Then, just to double check things, let's try running the app. 32 00:02:09,550 --> 00:02:13,880 Don't forget to select app from the drop down at the top before running it. 33 00:02:18,097 --> 00:02:20,880 Once it runs, it looks like we got an error. 34 00:02:22,980 --> 00:02:28,991 And if we look in the logcat, And 35 00:02:28,991 --> 00:02:36,180 filter by errors, We can find the error. 36 00:02:36,180 --> 00:02:41,400 Cannot access database on the main thread since it may potentially lock the UI for 37 00:02:41,400 --> 00:02:43,470 a long period of time. 38 00:02:43,470 --> 00:02:48,380 To fix this, every time we access our database we need to do it on a new thread. 39 00:02:50,010 --> 00:02:56,400 Let's add a line above our foreach loop and then wrap it inside a thread block. 40 00:03:01,930 --> 00:03:06,521 If you've not used Kotlin before this is just a nice shortcut to let us 41 00:03:06,521 --> 00:03:09,460 quickly create new threads. 42 00:03:09,460 --> 00:03:11,050 Now let's try running the app again. 43 00:03:16,503 --> 00:03:18,419 Okay? 44 00:03:23,832 --> 00:03:29,840 Looks like we're getting a UNIQUE constraint failed on our Topping.id. 45 00:03:29,840 --> 00:03:34,160 And if we look back at the app class we can see that each time we run the app 46 00:03:34,160 --> 00:03:38,320 we're inserting the same toppings each time. 47 00:03:38,320 --> 00:03:41,140 Or rather we're trying to insert them. 48 00:03:41,140 --> 00:03:45,920 But it's not letting us because these toppings are already in the database. 49 00:03:45,920 --> 00:03:48,450 To fix this let's head over to topping dow. 50 00:03:54,812 --> 00:03:58,502 And after the Insert annotation, let's add () and 51 00:03:58,502 --> 00:04:01,213 then specify an on conflict strategy. 52 00:04:01,213 --> 00:04:05,694 onConflict = and let's set it = 53 00:04:05,694 --> 00:04:10,340 OnConflictStrategy.replace. 54 00:04:11,750 --> 00:04:17,110 Now if we try to insert the same topping twice instead of throwing an error 55 00:04:17,110 --> 00:04:19,990 it will just replace that row in the database. 56 00:04:19,990 --> 00:04:20,830 Perfect. 57 00:04:20,830 --> 00:04:22,220 Let's try running the app again. 58 00:04:29,130 --> 00:04:30,368 And there we go. 59 00:04:30,368 --> 00:04:32,590 The app ran successfully. 60 00:04:32,590 --> 00:04:35,340 Now that we've got our database ready with our toppings, 61 00:04:35,340 --> 00:04:37,880 we can handle saving pizzas in the next video.