获取 Android Room 迁移中的上下文

huangapple go评论90阅读模式
英文:

Get Context in Android Room Migration

问题

Sure, here is the translated content:

在Android Room迁移中获取上下文:

在我的迁移中,我想获取上下文以从资产中读取一些文件:

  1. @Module
  2. @InstallIn(SingletonComponent::class)
  3. object
  4. PersistenceModule : Application() {
  5. @Provides
  6. @Singleton
  7. fun provideAppDatabase(
  8. @ApplicationContext context: Context,
  9. ): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "app")
  10. .addMigrations(MIGRATION_3_4)
  11. .build()
  12. }
  1. val MIGRATION_3_4 = object : Migration(3, 4) {
  2. override fun migrate(database: SupportSQLiteDatabase) {
  3. //...
  4. val txt = context.resources.assets.open("example.txt").bufferedReader().use { it.readText() }
  5. }
  6. }

Please note that the code remains in Kotlin, and I've translated the comments and string literals.

英文:

Get Context in Android Room Migration:

In my Migration i want to get context to read some files from assets:

  1. @Module
  2. @InstallIn(SingletonComponent::class)
  3. object
  4. PersistenceModule : Application() {
  5. @Provides
  6. @Singleton
  7. fun provideAppDatabase(
  8. @ApplicationContext context: Context,
  9. ): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "app")
  10. .addMigrations(MIGRATION_3_4)
  11. .build()
  12. }
  1. val MIGRATION_3_4 = object : Migration(3, 4) {
  2. override fun migrate(database: SupportSQLiteDatabase) {
  3. //...
  4. val txt = context.resources.assets.open("example.txt").bufferedReader().use { it.readText() }
  5. }
  6. }

答案1

得分: 1

Create a class extending Migration which takes context as parameter in constructor

  1. class Migration3to4(@ApplicationContext val context: Context): Migration(3, 4) {
  2. override fun migrate(database: SupportSQLiteDatabase) {
  3. // access context here
  4. val txt = context.resources.assets.open("example.txt").bufferedReader().use { it.readText() }
  5. }
  6. }

Callsite

  1. @Module
  2. @InstallIn(SingletonComponent::class)
  3. object PersistenceModule {
  4. @Provides
  5. @Singleton
  6. fun provideAppDatabase(
  7. @ApplicationContext context: Context,
  8. ): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "app")
  9. .addMigrations(Migration3to4(context))
  10. .build()
  11. }
英文:

Create a class extending Migration which takes context as parameter in constructor

  1. class Migration3to4(@ApplicationContext val context: Context): Migration(3.4) {
  2. override fun migrate(database: SupportSQLiteDatabase) {
  3. // access context here
  4. val txt = context.resources.assets.open("example.txt").bufferedReader().use { it.readText() }
  5. }
  6. }

Callsite

  1. @Module
  2. @InstallIn(SingletonComponent::class)
  3. object
  4. PersistenceModule : Application() {
  5. @Provides
  6. @Singleton
  7. fun provideAppDatabase(
  8. @ApplicationContext context: Context,
  9. ): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "app")
  10. .addMigrations(Migration3to4(context))
  11. .build()
  12. }

huangapple
  • 本文由 发表于 2023年5月25日 06:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定