Get immediate access to this and 4,000+ other videos and books.

Take your career further with a raywenderlich.com Pro subscription. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it’s simply the best investment you can make in your development career.

Model the Repository Pro

The version of the architecture components to add is 2.5.0. Check the download materials for the updated ViewModel and LiveData dependencies.

suspend should be added to the saveCreature and clearAllCreatures methods definition in the CreatureRepository interface. So the interface should look like this:

interface CreatureRepository {
  suspend fun saveCreature(creature: Creature)
  fun getAllCreatures(): LiveData<List<Creature>>
  suspend fun clearAllCreatures()
}

The RoomRepository class no longer uses AsyncTask. Instead it used Kotlin Coroutines. Check the download materials for the updated code or take a look at what it looks like below:

class RoomRepository : CreatureRepository {

  private val creatureDao: CreatureDao = CreaturemonApplication.database.creatureDao()

  private val allCreatures: LiveData<List<Creature>>

  init {
    allCreatures = creatureDao.getAllCreatures()
  }

  override suspend fun saveCreature(creature: Creature) {
    creatureDao.insert(creature)
  }

  override fun getAllCreatures() = allCreatures

  override suspend fun clearAllCreatures() {
    val creatureArray = allCreatures.value?.toTypedArray()
    if (creatureArray != null) {
      creatureDao.clearCreatures(*creatureArray)
    }
  }
}

Create a repository interface for saving your Model data, and implement a concrete version of the repository using the Room database library.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

In this video, we’re going to setup a Repository in which we’ll save our Model data. Our concrete repository is going to be a Room database, and we’re going to use LiveData from the Android Architecture Components library to retrieve data from the database.

arch_comp_version = '1.1.1'
  // ViewModel and LiveData
  implementation "android.arch.lifecycle:extensions:$arch_comp_version"
interface CreatureRepository {
  fun saveCreature(creature: Creature)
  fun getAllCreatures(): LiveData<List<Creature>>
  fun clearAllCreatures()
}
@Entity(tableName = "creature_table")
data class Creature(
    val attributes: CreatureAttributes = CreatureAttributes(),
    val hitPoints: Int = 0,
    @PrimaryKey @NonNull val name: String,
    val drawable: Int = 0
)
interface CreatureDao {
   @Insert(onConflict = OnConflictStrategy.REPLACE)
  fun insert(creature: Creature)
   @Delete
  fun clearCreatures(vararg creature: Creature)
   @Query("SELECT * FROM creature_table ORDER BY name ASC")
  fun getAllCreatures(): LiveData<List<Creature>>
}
@TypeConverters(CreatureAttributesConverter::class)
  private val allCreatures: LiveData<List<Creature>>
   init {
    allCreatures = creatureDao.getAllCreatures()
  }

   override fun saveCreature(creature: Creature) {
    InsertAsyncTask(creatureDao).execute(creature)
  }
   override fun getAllCreatures() = allCreatures
   override fun clearAllCreatures() {
    val creatureArray = allCreatures.value?.toTypedArray()
    if (creatureArray != null) {
      DeleteAsyncTask(creatureDao).execute(*creatureArray)
    }
  }
dao.clearCreatures(*params)
    database = Room.databaseBuilder(this, CreatureDatabase::class.java, "creature_database")
        .build()

Reviews

Comments