问题描述
在我的应用程序中,我首先使用改造从服务器上获取数据,然后将其保存在房间数据库表中,然后在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.
Movie.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>
}
MovieDatabase.kt
MovieDatabase.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" annotateenter image description here
(2)当id为"autoGenerate = true"的主键时;注释在此处输入图片描述
(2)when id as a primary key with "autoGenerate = true" annotateenter 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,但是在刷新数据库时它会显示两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!