Hilt实例化的defaultDispatche在哪里



代码A来自官方示例项目的最终分支。

该项目使用Hilt来实现依赖项注入。

我搜索了整个项目,只找到了代码annotation class DefaultDispatcher,但我不明白代码的意思。

Hilt实例化的defaultDispatche在哪里?

代码A

@HiltViewModel
class MainViewModel @Inject constructor(
private val destinationsRepository: DestinationsRepository,
@DefaultDispatcher private val defaultDispatcher: CoroutineDispatcher
) : ViewModel() {
...
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun CraneHomeContent(
onExploreItemClicked: OnExploreItemClicked,
openDrawer: () -> Unit,
modifier: Modifier = Modifier,
viewModel: MainViewModel = viewModel(),
) {
...
}

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class DefaultDispatcher

您应该有一个像这样声明的模块

@Module
object DispatcherModule {
@DefaultDispatcher
@Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default

}

现在创建这个类是为了使用@DefaultDispatcher作为注释

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class DefaultDispatcher

有关更多详细信息,请访问此处

在你的示例项目中,你分享的内容是这样写的。

这意味着@DefaultDispatcher将被用作注释,其中值将由DispatcherModule提供

最新更新