使用带有挂起功能块的接口类的 Hilt 进行依赖注入

Oli*_*liv 3 android kotlin kotlin-coroutines dagger-hilt

我声明一个使用挂起函数的类。这个类是由 Hilt 库在 Android 上注入的单例依赖项:

interface Foo {
   suspend fun send(v: Int)
   suspend fun receive(): Int
}
class FooImpl @Inject () {
   var channel: Channel<Int>(2, BufferOverflow.DROP_OLDEST)
   override suspend fun send(v: Int) {
       channel.send(v)
   }
   override suspend fun receive(): Int {
       channel.receive()
   }
}

//FIRST CASE:
@Module
@InstallIn(SingletonComponent::class)
abstract class FooModule {
  @Binds
  abstract fun bindFoo(foo: FooImpl): Foo
 }
Run Code Online (Sandbox Code Playgroud)

然后,如果当我调用接收函数时,它会永远被阻止。未收到数据,示例:

@AndroidEntryPoint
class Bar: Service {
  @Inject
  lateinit var foo: Foo
  private val scope = CoroutineScope(Job() + Dispatchers.Default)
  //...
  fun doSomething() {
    scope.launch() {
      foo.receive()
      //coroutine execution never reach this line
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在这个简单的情况下,就像Foo单例一样,我可以实现一个简单的解决方法。如果 Foo 在 Hilt 中以这种方式实现,我就没有问题:

//SECOND_CASE:
val FOO: Foo = FooImpl()

@Module
@InstallIn(SingletonComponent::class)
object FooModule {
   @Provides
   fun providesFoo(): Foo {
      return FOO
   }
}
Run Code Online (Sandbox Code Playgroud)

我想知道这是一个 Hilt bug 还是我的 FIRST_CASE hilt 模块实现是错误的?

Adr*_*n K 5

您从不声明FooImpl为单例,因此每次注入时,您都会得到一个新的实例。
如果您认为是这样的话@InstallIn(SingletonComponent::class),那么事实并非如此。此注释仅告诉 hiltFooModule 本身应该是单例,并且范围不限于 Activity、ViewModel 或 Fragment 的生命周期。

您需要添加@SingletonFooImpl绑定它的方法:

选项1

interface Foo {
   suspend fun send(v: Int)
   suspend fun receive(): Int
}

@Singleton
class FooImpl @Inject constructor() : Foo {
   ...
}

@Module
@InstallIn(SingletonComponent::class)
abstract class FooModule {
    @Binds
    abstract fun bindFoo(foo: FooImpl): Foo
}

Run Code Online (Sandbox Code Playgroud)

选项2

interface Foo {
   suspend fun send(v: Int)
   suspend fun receive(): Int
}

class FooImpl @Inject constructor() : Foo {
   ...
}

@Module
@InstallIn(SingletonComponent::class)
abstract class FooModule {
    @Singleton
    @Binds
    abstract fun bindFoo(foo: FooImpl): Foo
}
Run Code Online (Sandbox Code Playgroud)