Android Kotlin 中的数据库存储库比较和/或更新?

问题描述

我是 Android 编程新手,想尝试学习最佳实践。我正在构建的第一个应用程序是一个播客应用程序,用于显示来自 RSS 提要的播客并播放它们。到目前为止,我正在工作,但我知道我可以让它更好地工作。

我正在使用具有存储库模式的 Room 数据库,这可能有点矫枉过正,因为如果我只是要在启动时重新解析提要,我可能不需要在应用程序死亡期间保留播客列表。在我的存储库类中,我正在调用我的 FetchRSS 类来在返回 init{ }List<podcast> 块中进行网络调用

我知道我做错了什么。

在我的 podcastDao 中,我必须使用 @Insert(onConflict = OnConflictStrategy.REPLACE),因为数据库已经存在并且我收到关于重复主键 ID 的 sql 错误 1555。从逻辑上讲,最好检查一下要添加的条目是否已经在数据库中,但我不确定如何去做。或者,不合逻辑地,在应用程序死亡时清除数据库,但是为什么要为数据库烦恼呢。理想情况下,我想要滑动更新功能(即使 RSS 每周最多更新两次),但我不确定如何最好地做到这一点。

如果有人对改进这一点有任何想法,或者是一本学习 android 的好书,我会全神贯注。 非常感谢所有花时间看这个的人!

podcastDao.kt

@Dao
interface podcastDao {

    @Query("SELECT * FROM podcast")  // get everything from the database
    fun getpodcasts(): LiveData<List<podcast>>

    @Query("SELECT * FROM podcast WHERE id=(:id)") // get the specific podcast
    fun getpodcast(id: String): LiveData<podcast?>

//    @Insert(onConflict = OnConflictStrategy.REPLACE)
//    fun addpodcasts(podcasts: LiveData<List<podcast>>)
//    this causes a build error with the generated podcastDao.java file
// logcat error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun addpodcast(podcast: podcast)
}

podcastRepository.kt

class podcastRepository private constructor(context: Context) {

private lateinit var podcasts: List<podcast>

    init {

        Coroutinescope(dispatchers.Main).launch {

            podcasts = FetchRSS().fetchRSS() // executes on dispatchers.IO and returns parsed RSS List<podcast>

            // this seems silly to add them one at a time,especially since the list is rather large
            for (pod in podcasts) {
                addpodcast(pod)
            }
            //it seems a better choice to dump the full list into the database at once
            //however I can't figure out how to put the List<podcast> into a LiveData<List<podcast>> object
            //or maybe I'm misunderstanding something about LiveData<>
            //addpodcasts(podcasts)
        }
    }

    suspend fun addpodcast(podcast: podcast){
        withContext(dispatchers.IO){
            podcastDao.addpodcast(podcast)
        }

// this needs to receive the LiveData<List<podcast>>,or a List<podcast> and cram it into LiveData<>?
//    suspend fun addpodcasts(podcasts: LiveData<List<podcast>>) {
//        withContext(dispatchers.IO){
//            podcastDao.addpodcasts(podcasts)
//        }
//    }
    }

解决方法

fun addPodcasts(podcasts: LiveData<List<Podcast>>)

应该

fun addPodcasts(podcasts: <List<Podcast>>)

因此,现在您可以从存储库内部调用 podcastDao.addPodcasts(podcasts)(其中 podcasts 的类型为 List<Podcast>>),而不是通过 for 循环一个一个地插入它们。

您不能将 LiveData 插入 Room,只能插入标有 @Entity 的对象。但是,您可以让查询返回带有这些实体列表的 LiveData。您也可以只返回一个列表。