我可以在房间数据库中自动增加 id 但是当刷新数据库时它显示双倍 [英] I can auto increment id in room database but when refresh database it's shows double

查看:24
本文介绍了我可以在房间数据库中自动增加 id 但是当刷新数据库时它显示双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,首先我使用改造从服务器获取数据,然后将其保存在房间数据库表中,然后显示在 recyclerview 中,但是当我使用 id 作为主键时,它只显示一个数据,然后这个 id 我注释 autoGenerate = true然后它显示我放入服务器的所有数据,但是当我重新打开我的应用程序时,它显示双倍数据(这意味着首先我在服务器中有 3 个数据,这个应用程序显示 3 个数据但是当我重新打开或刷新我的数据库时它显示 6 个数据,每次刷新它都会增加).但我希望这个 id 会增加但数据不会增加,这意味着服务器有 3 个数据并且它存储在房间数据库表中.

In my app firstly i am fetching data from server using retrofit then it's saved in room database table then it shows in recyclerview but when i using a id as a primary key it's show only one data then this id i annotat autoGenerate = true then it's show all data which i put in server but when i reopen my app,it's show double data(this mean firstly i have 3 data in server,this app show 3 data but when i reopen or refresh my database it's show 6 data ,every time in refresh it's increasing).but i want this id will be increase but data will be not increase that's mean server have 3 data and it's store in room database table.

电影.kt

@Entity(tableName = "movie_table")
data class Movie(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
@SerializedName("Title")
@Expose
val title: String,
@SerializedName("Year")
@Expose
val Year: Int,
@SerializedName("Genre")
@Expose
val genre: String,
@SerializedName("Language")
@Expose
val language: String,
@SerializedName("Country")
@Expose
val country: String,
@SerializedName("Poster")
@Expose
val poster: String,
@SerializedName("Plot")
@Expose
val plot:String
)

MovieDao.kt

MovieDao.kt

@Dao
interface MovieDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertMovie(movie: Movie)

@Query("SELECT * FROM movie_table")
suspend fun getAllMovieDB(): List<Movie>

@Query("SELECT * FROM movie_table WHERE title LIKE :title")
suspend fun getSearchMovieDB(title: String): List<Movie>
}

电影数据库.kt

@Database(
entities = [Movie::class],
version = 1)
abstract class MovieDatabase: RoomDatabase() {
abstract fun getMovieDao(): MovieDao

companion object{
    @Volatile
    private var instance : MovieDatabase? = null
    private val LOCK = Any()
    operator fun invoke(context: Context) = instance
        ?: synchronized(LOCK){
            instance
                ?: buildDatabase(context).also{
                    instance = it
                }
        }
    private fun buildDatabase(context: Context) = Room.databaseBuilder(
        context.applicationContext,
        MovieDatabase::class.java,
        "movieDatabase"
    ).build()
}
}

Repository.kt

Repository.kt

class Repository(context: Context) {
private val movieDao: MovieDao = MovieDatabase.invoke(context).getMovieDao()
private val movieCall = MovieApiClient.movieApi
suspend fun insertMovie(movie: Movie) {
    movieDao.insertMovie(movie)
}
suspend fun searchData(title: String): List<Movie> {
    return movieDao.getSearchMovieDB(title)
}
suspend fun getAllMovieDB(): List<Movie> {
    return movieDao.getAllMovieDB().also {
        getAllMovieFromServer()
    }
}
private suspend fun getAllMovieFromServer() {
    try {
        val movieList = movieCall.getAllMovie()
        movieList.forEach {
            insertMovie(it)
        }
    } catch (exception: Throwable) {
    }
}
}

更好地理解图像(1) 当 id 作为没有autoGenerate = true"的主键时;注释在此处输入图片描述

better understanding with image (1)when id as a primary key without "autoGenerate = true" annotate enter image description here

(2) 当 id 作为主键并且autoGenerate = true"时;注释在此处输入图片描述

(2)when id as a primary key with "autoGenerate = true" annotate enter image description here

如何在没有重复数据的情况下增加 id,我该如何解决?请帮帮我,谢谢.

How can I increase id without double data, How can i fix this? Please help me, Thank you.

推荐答案

看起来您没有从服务器获取电影 ID.这意味着您无法知道特定电影何时已存在于您的数据库中,这就是它被复制"的原因.

Looks like you are not fetching the movie ID from the server. This means that there is no way for you to know when a specific movie already exists in your DB, which is why it gets "duplicated".

要解决此问题,只需确保从服务器获取电影 ID 以及所有其他电影数据.并删除autoGenerate = true";因为您将使用自己的 ID.

To fix this simply make sure to fetch the movie ID from the server, too, along with all the other movie data. And remove the "autoGenerate = true" since you'll be using your own IDs.

这将确保当从服务器获取已存在于您的数据库中的电影时,插入查询中会发生冲突,并且由于您使用了(onConflict = OnConflictStrategy.REPLACE)";它将用新数据更新该行.

This will ensure that when a movie that already exists in your DB is fetched from the server, there will be a conflict in the insert query, and since you used "(onConflict = OnConflictStrategy.REPLACE)" it will update the row with the new data.

这篇关于我可以在房间数据库中自动增加 id 但是当刷新数据库时它显示双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆