Dispatchers should be injectable
Code smell
Major
kotlin:S6310
Dispatchers should not be hardcoded when using withContext or creating new coroutines using launch or async. Injectable dispatchers ease testing by allowing tests to inject more deterministic dispatchers.
You can use default values for the dispatcher constructor arguments to eliminate the need to specify them explicitly in the production caller contexts.
This rule raises an issue when it finds a hard-coded dispatcher being used in withContext or when starting new coroutines.
Noncompliant Code Example
class ExampleClass {
suspend fun doSomething() {
withContext(Dispatchers.Default) { // Noncompliant: hard-coded dispatcher
...
}
}
}
Compliant Solution
class ExampleClass(
private val dispatcher: CoroutineDispatcher = Dispatchers.Default
) {
suspend fun doSomething() {
withContext(dispatcher) {
...
}
}
}
See
Inject dispatchers (Android coroutines best practices)
https://developer.android.com/kotlin/coroutines/coroutines-best-practices#inject-dispatchers
반응형
'SW Programming > Android' 카테고리의 다른 글
안드로이드 recyclerView 내부 영역 클릭 강제로 발생시키기(feat.chatGPT) (0) | 2023.02.21 |
---|---|
[안드로이드] 카드뷰, 무한로딩, View pager transformation (0) | 2022.08.18 |
[Android] 중첩된 리스트 사용시 스크롤 민감도 조절해보기(가로, 세로 스크롤) (0) | 2022.05.20 |
Android 단말에서 온갖 이벤트를 모니터링, 제어하고 싶다. adb shell getevent (0) | 2021.03.23 |
IOS 안드로이드 스튜디오 업데이트 오류 해결 (0) | 2017.06.15 |
댓글