From the course: Android Development Essential Training: Manage Data with Kotlin

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Insert and retrieve data with Room

Insert and retrieve data with Room

From the course: Android Development Essential Training: Manage Data with Kotlin

Start my 1-month free trial

Insert and retrieve data with Room

- [Instructor] Once you've defined a database with Room, you can use it from anywhere in your app. But I've architected my app so that direct database access is only done from one class, my repository. So all the other changes are going to be made here, in my Monster Repository Class. First, I'll change my init block. Right now, I'm calling this function readDataFromCache. This is reading a text file with JSON formatted data and using Moshi to parse that data. I'm going to delete all that code and up here I'll create an instance of my data access object. I'll call it monsterDao and I'll initialize it with this expression. MonsterDatabase.getDatabase I'll pass in my application as the context and then from there, I'll call MonsterDao. And now I have an object I can use in memory to work with that database table. In the init function I'll create a co-routine so that I'm doing all this work in a background thread. I'll pass Dispatchers.IO and then from there call the launch function…

Contents