1

I'm kind of new to Android development, but I'm taking over some project somebody more experimented than me did.

I'm making an application that retrieves data from the internet at startup and creates (or updates) a room database to store the information.

So far, it works. However, I noticed that when I push an update of the application, for some users, the database doesn't update anymore. They can reinstall the app, it doesn't change anything. The only thing that works is to clear the cache and restart the application. Then everything goes back to normal, when data are retrieved from the internet, they are properly inserted in the database. But the problem comes back with the next update.

I added the 'fallbackToDestructiveMigration()' option but it doesn't help, because it's not a migration of the database per se, as the structure doesn't change here.

Ideally, I'd like to preserve the data already present in the database.

I'm using Room 2.2.5 and API 28.

I'm not sure why updating the app results in the database not updating anymore. Maybe the new version of the app creates another database and populates this one, but the app is still using the old one (not updated anymore).

The Storage Module:

val storageModule = module {
    single {
        Room.databaseBuilder(androidContext(), LessonDatabase::class.java, "MyDB")
            .fallbackToDestructiveMigration().build()
    }

    single {
        get<LessonDatabase>().lessonDao()
    }
}

The LessonDatase:

@Database(entities = [Lesson::class], version = BuildConfig.DATABASE_VERSION, exportSchema = false)
abstract class LessonDatabase : RoomDatabase() {
    abstract fun lessonDao(): LessonDao
}

The LessonDao:

@Dao
interface LessonDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertLesson(lesson: Lesson)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insertLessons(lessons: List<Lesson>)

    @Update
    fun updateLesson(lesson: Lesson)

    @Delete
    fun deleteLesson(lesson: Lesson)

    @Query("DELETE FROM Lesson")
    fun clearLessons()

    @Query("SELECT * FROM Lesson WHERE id == :id")
    fun getLessonById(id: String): Lesson

    @Query("SELECT * FROM Lesson ORDER BY creation_date DESC")
    fun getLessons(): List<Lesson>

    @Query("SELECT * FROM Lesson WHERE favorite = 1")
    fun getFavoriteLessons(): List<Lesson>

    @Query("SELECT * FROM Lesson WHERE difficulty LIKE :level")
    fun getLessonsByDifficulty(level: Int): List<Lesson>
}

And the code for the application startup:

class SplashscreenViewModel(
    private val repository: LessonRepository,
    private val lessonDao: LessonDao,
    val context: Context
) : BaseViewModel() {

    val nextScreenLiveData = MutableLiveData<Boolean>()

    override fun start() {

        ioScope.launch {

            val lessons = repository.getLessons().filter {
                it.site.contains("website")
            }.filter {
                DataUtils.isANumber(it.id)
            }

            lessonDao.insertLessons(lessons)
            nextScreenLiveData.postValue(true)
        }
    }

    override fun stop() {
    }
}

A question I have is, if I update the application, I guess Room.databaseBuilder will be called again. But what happens if the name of the database is the same as the previous one? Will it retrieve the old one, or create a new one? Overwrite the old one?

Another question I have, in the Insert query, it says onConflictStrategy.IGNORE. But as I pass a list as parameters, what happens if some of the entries are already in the database and some not? Will it ignore all of them? Just the already existing ones?

Thank you.

Edit: I installed Android-Debug-Database (https://github.com/amitshekhariitbhu/Android-Debug-Database) and it seems the database is fine actually. The only problem is that when I update the app, the new entries I insert are returned at the end of the SELECT * FROM table query. So I tried to sort them by Id, and it seems to work.

2
  • BuildConfig.DATABASE_VERSION, do you update that number when you push a new version and the database scheme has changed?
    – Eselfar
    Commented Nov 18, 2020 at 4:31
  • The database scheme doesn't change. All that change is the content of the database, but the structure remains the same. Commented Nov 18, 2020 at 5:07

0