在Android房间迁移中获取上下文:
在我的迁移中,我想获取上下文以从assets资源 中读取一些文件:
@Module
@InstallIn(SingletonComponent::class)
object
PersistenceModule : Application() {
@Provides
@Singleton
fun provideAppDatabase(
@ApplicationContext context: Context,
): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "app")
.addMigrations(MIGRATION_3_4)
.build()
}
val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
//...
val txt = context.resources.assets.open("example.txt").bufferedReader().use { it.readText() }
}
}