Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room database Insert or update but keep a database field?

I am trying to achieve the following scenario -> Records are fetched from API and stored into DB. Some of the records might be clicked as favorite and they should stay like that even if new data is fetched from API. But, I can't seem to find a way on how to not replace those records that already have been selected as favorite without replacing the whole row. I've tried inserting the records one by one and somehow in that insertion check if a record already exists but I can't seem to figure it out using RxJava.

Dao

    @Dao
    interface KafanaDao {

    @Insert
    fun insertSingleKafana(kafana: Kafana)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(kafani: List<Kafana>)

    @Query("SELECT * FROM ${Constants.KAFANI_TABLE_NAME}")
    fun getKafani(): Single<List<Kafana>>

    @Query("SELECT * FROM ${Constants.KAFANI_TABLE_NAME} WHERE name = :name")
    fun getSingleKafana(name: String): Single<Kafana>

    @Query("UPDATE ${Constants.KAFANI_TABLE_NAME} SET isFavorite = :isFavorite WHERE name = :name")
    fun setFavourite(name: String, isFavorite: Int)
}

Then I keep inserting the records one by one, but how do I check if they are already stored in the database?

fun insertSingleKafanaInDb(kafana: Kafana) {
    Observable.fromCallable { kafanaDao.insertSingleKafana(kafana) }
            .subscribeOn(Schedulers.io())
            .subscribe {
                Timber.d("Inserted ${kafana.name} kafani from API in DB...")
            }
}

fun getKafaniFromApi(): Observable<List<Kafana>> {
    return apiService.getKafani().toObservable().doOnNext {
        for (kafana in it) {
            //I need to somehow do the check here
            insertSingleKafanaInDb(kafana)
        }
    }
}

Entity

    @Entity(tableName = Constants.KAFANI_TABLE_NAME)
    data class Kafana(
        @PrimaryKey
        @ColumnInfo(name = "name")
        @SerializedName("name")
        val name: String,
        @ColumnInfo(name = "phone")
        @SerializedName("phone")
        val phone: String,
        @ColumnInfo(name = "address")
        @SerializedName("address")
        val address: String,
        @ColumnInfo(name = "city")
        @SerializedName("city")
        val city: String,
        @ColumnInfo(name = "sponsored")
        @SerializedName("sponsored")
        val isSponsored: Boolean,
        @ColumnInfo(name = "isFavorite")
        var isFavorite: Boolean

)
like image 564
Esteban Avatar asked Jan 04 '18 17:01

Esteban


1 Answers

Can be done by two approach

First One:- Before insert Kafane you query getSingleKafana(name: String): Single and get it from the database if it's available and get the favorite status and set this favorite field to newly come KAFANA object from API.

Second One: Do a query where it will return List of KAFANA objects with a favorite is true, the result will List, before inserting new KAFANA from API check kafana is presented in List.

Room wont support any conditional insert query. It will insert the entire object.

like image 156
lib4 Avatar answered Nov 02 '22 05:11

lib4