DB에 미리 채워진 데이터베이스가 필요해서 db browser for sqlite 를 사용해서
DB 파일을 생성한 뒤에
코드에서 createFromAsset을 사용해서 패키지 아래 db 파일을 DB로 사용하려고 하고 있습니다.
만든 DB 파일은 아래와 같습니다.

그리고 아래와 같은 에러를 얻었습니다
E/AndroidRuntime: FATAL EXCEPTION: arch_disk_io_0
Process: com.example.lightweight, PID: 3488
java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData$ 1 .run(RoomTrackingLiveData.java: 92 )
at androidx.room.TransactionExecutor$ 1 .run(TransactionExecutor.java: 47 )
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1167 )
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java: 641 )
at java.lang.Thread.run(Thread.java: 920 )
Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
at androidx.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java: 154 )
at androidx.room.RoomOpenHelper.onOpen(RoomOpenHelper.java: 135 )
|
Room cannot verify the data intergrity. ~~ 인데
스택오버플로우에 검색해보니 저랑 같은 현상 겪는 사람들이 많더라구요. (그 사람들은 createFromAsset 방법을 사용한것같지는 않고 다른 방법을 사용한것 같습니다.)
아무튼 해결책으로 삭제/재설치를 해보라고해서 해보고 매니페스트에서 allowBackup~~ = false를 해보라 해서 시도해보는데 계속 같은 현상이 일어나는데, 어떠한 이유 떄문일까요?
---
WorkoutApplication
class WorkoutApplication : Application() {
val database by lazy { WorkoutDatabase.getDatabase( this ) }
val workoutListRepo: WorkoutListRepository by lazy { WorkoutListRepository(database.workoutDao()) }
}
|
WorkoutDatabase
@Database (
entities = [Workout:: class , WorkoutSetInfo:: class , WorkoutListTest:: class ],
version = 1
)
@TypeConverters (UnitConverter:: class , WorkoutListTypeConverter:: class )
abstract class WorkoutDatabase : RoomDatabase() {
abstract fun workoutDao() : WorkoutDao
companion object {
private var INSTANCE : WorkoutDatabase? = null
@Synchronized
fun getDatabase(context: Context): WorkoutDatabase {
return INSTANCE ?: synchronized ( this ) {
val instance = Room.databaseBuilder(
context.applicationContext,
WorkoutDatabase:: class .java,
"workout_database"
)
.createFromAsset( "database/workout_database.db" )
.build()
INSTANCE = instance
instance
}
}
}
}
|
Dao
@Dao
interface WorkoutDao {
@Query ( "SELECT * From WorkoutListTest" )
fun getWorkoutListTest() : List<WorkoutListTest>
@Query ( "SELECT * FROM Workout" )
@Transaction
fun getWorkoutSetInfoList() : LiveData<List<WorkoutWithSets>>
@Insert (onConflict = OnConflictStrategy.REPLACE)
fun insertWorkout(workout: Workout) : Long
@Insert (onConflict = OnConflictStrategy.REPLACE)
fun insertSetInfoList(list: List<WorkoutSetInfo>)
@Insert
fun insertAll(list: List<Workout>)
@Delete
fun delete(detail: Workout)
@Update
fun update(list: List<Workout>)
}
|
ViewModel
class WorkoutListViewModel(val repository: WorkoutListRepository) : ViewModel(){
private var _list = MutableLiveData<List<String>>(arrayListOf())
val list: LiveData<List<String>>
get() = _list
fun getList(part: BodyPart) {
viewModelScope.launch(Dispatchers.IO) {
val result = repository.getWorkoutList(part)
_list.postValue(result)
}
}
}
|
Repository
class WorkoutListRepository( private val workoutListDao: WorkoutDao) {
fun getWorkoutList(part: BodyPart) : List<String> {
val test: List<WorkoutListTest> = workoutListDao.getWorkoutListTest()
val newList = test.map {
it.workout
}
return when(part) {
is BodyPart.Chest -> newList
is BodyPart.Back -> newList
is BodyPart.Leg -> newList
is BodyPart.Shoulder -> newList
is BodyPart.Biceps -> newList
is BodyPart.Triceps -> newList
is BodyPart.Abs -> newList
}
}
}
|