Coroutine(4)
-
Kotlin Coroutine - LiveData, Flow, StateFlow, SharedFlow, stateIn, sharedIn
목표 LiveData와 stateFlow & sharedFlow의 차이점 이해 StateFlow와 SharedFlow의 차이점과 사용방법 이해 stateIn과 sharedIn 이해 Coroutine Flow Flow란 Flow는 단일 값만 반환하는 suspend 함수와 달리 여러 값을 순차적으로 내보낼 수 있는 유형 비동기식으로 계산할 수 있는 데이터 스트림의 개념 Flow는 cold stream 이다 stream? Cold Stream 구독자가 하나 존재하며 해당 구독자에게만 값을 방출 또한 해당 구독자는 스트림을 초기화하는 구독자 즉, 다른 구독자가 또 생겨날 경우 구독 시점은 다르나 데이터는 동일하다 Lazy Stream으로 누군가가 구독할 때만 값의 방출이 시작 Hot Stream Consumer..
2022.01.27 -
[Coroutine Flow] stateFlow 로 ui 업데이트시 주의 사항
1. stateFlow 생성 class LatestNewsViewModel( private val newsRepository: NewsRepository ) : ViewModel() { // Backing property to avoid state updates from other classes private val _uiState = MutableStateFlow(LatestNewsUiState.Success(emptyList())) // The UI collects from this StateFlow to get its state updates val uiState: StateFlow = _uiState init { viewModelScope.launch { newsRepository.favorite..
2022.01.27 -
[Android Kotlin Coroutine] 코루틴 기본정리 (CoroutineContext, Coroutine Builder, CoroutineScope, Coroutine Job, CoroutineDispatcher)
Coroutine 비동기적으로 실행되는 코드를 간소화하기 위해 Android에서 사용할 수 있는 동시 실행 설계 패턴 CoroutineContext interface CoroutineContext CoroutineContext는 다양한 요소의 집합으로 코루틴의 동작을 정의합니다. Job: 코루틴의 수명 주기 제어 CoroutineDispatcher: 적절한 스레드에 작업 전달 CoroutineName: 디버깅에 유용한 코루틴 이름 CoroutineExceptionHandler: 포착되지 않은 예외 처리 Coroutine Builder (launch, async)는 CoroutineContext 값을 optional로 지정할 수 있고 이를 통해 dispatcher 를 지정할 수 있다. fun main() ..
2022.01.18 -
[AAC/Room] Exception - Cannot access database on the main thread since it may potentially lock the UI for a long period of time. 해결 방법
1. Exception 확인 java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time. 2. 발생 원인 Room 사용 시 쿼리 호출을 MainThread에서 실행했을 경우 발생 3. 해결 방법 allowMainThreadQueries() 메서드를 사용하면 간단하게 해결할 수 있지만 메인 스레드에서 많은 양의 쿼리문을 실행하다 보면 ui가 오랜 기간 동작하지 않을 수 있으므로 사용하면 안 되는 방법이다! 이외에 AsyncTask, RxJava, Coroutine, JavaThread를 사용하여 해결할 수 있습니다 오늘 ..
2020.06.10