깨알 개념 121

[Kotlin] Coroutines Flow - StateFlow

#1 개요 StateFlowA SharedFlow that represents a read-only state with a single updatable data value that emits updates to the value to its collectors. A state flow is a hot flow because its active instance exists independently of the presence of collectors. Its current valukotlinlang.orgSharedFlow의 일종인 StateFlow에 대해 살펴본다. #2 SharedFlow와의 비교이름에서 알 수 있듯, StateFlow는 기존 SharedFlow에 State의 특성을 결합한 Flow다..

[Kotlin] Coroutines Flow - Intermediate operator

#1 개요#1-1 중간 연산자 (Intermediate operator) Asynchronous Flow | Kotlin kotlinlang.org중간 연산자에 대해 살펴본다. #1-2 중간 연산자의 구조중간 연산자는 이름 그대로의 역할을 수행한다. 이름에 있는 '중간'은, emit과 collect 사이 '중간'을 의미한다. 시작점이 emit이고 도착점이 collect인 배수관 속 물의 흐름을 intermediate operator가 하이재킹하는 느낌이라고 보면 된다. 여기서 중요한 점은, 앞으로(=미래에) emit될 데이터를 변형하는 연산자는 결코 아니라는 것이다. 이미(=과거에) emit된 데이터에 특정 연산을 가하여, collect에 전달하는 연산자다. 중간 연산자이니 말이다. #2 Flow에 가용..

[Kotlin] Coroutines Flow - Back pressure와 그 처리

#1 개요Coroutines Flow를 사용할 때 생길 수 있는 현상인 백 프레셔(Back pressure)에 대해 살펴본다. 또, 백 프레셔를 처리하는 방법도 살펴본다. 이 때, 백 프레셔는 에러가 아닌 자연스러운 현상일 뿐이다. 따라서, 백 프레셔를 해결한다는 표현은 정확하지 않다. 백 프레셔에 대처한다는 표현이 옳다. #2 백 프레셔#2-1 백 프레셔가 없는 코드import kotlinx.coroutines.*import kotlinx.coroutines.flow.Flowimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.flow// 1초마다 1씩 증가하는 countfun startCountUp(count: Mu..

Cold Flow와 Hot Flow (SharedFlow)

#1 Cold Flow와 Hot Flow#1-1 개요데이터 스트림은 크게 Cold Flow와 Hot Flow로 나눌 수 있다. 이 분류 기준에 대해 알아본다. 또, Kotlin의 Coroutines Flow를 활용해 간단한 Cold Flow 및 Hot Flow를 구현해본다.  #1-2 훌륭한 비유 What is the hot flow and cold flow in coroutines and the difference between them?I am mastering Kotlin coroutines and trying to figure out 1- what is hot flow and cold flow ? 2- what is the main difference between them? 3- when to ..

[Android] Coroutines Flow - Jetpack Compose에서 사용하기

#1 개요 [Kotlin] Coroutines Flow - 기초#1 Coroutines Flow#1-1 개요 FlowFlow An asynchronous data stream that sequentially emits values and completes normally or with an exception. Intermediate operators on the flow such as map, filter, take, zip, etc are functions that are applied to the upstkenel.tistory.com위에서 다룬 Coroutines Flow를 Jetpack Compose에 적용시켜본다. 여기서 말하는 '적용'이란, 단순한 사용이 아니라 Flow 객체를 State로서 ..

[Kotlin] Coroutines Flow - 기초

#1 Coroutines Flow#1-1 개요 FlowFlow An asynchronous data stream that sequentially emits values and completes normally or with an exception. Intermediate operators on the flow such as map, filter, take, zip, etc are functions that are applied to the upstream flow or flows and return a dokotlinlang.orgFlow는 내부적으로 Coroutine을 사용해 비동기적으로 데이터 스트림을 처리하는 API다. 이를 반응형 프로그래밍이라고도 한다. 반응형 프로그래밍을 한 마디로 정의하면, ..

[Kotlin] Coroutines - 한 Scope 내에서의 계층 관계

#1 이전 글 [Kotlin] Coroutines - Coroutine builder#1 Coroutine builder kotlinx-coroutines-coreCore primitives to work with coroutines. Coroutine builder functions: Coroutine dispatchers implementing CoroutineDispatcher: More context elements: Synchronization primitives for coroutines: Top-level suspendinkenel.tistory.com위 게시글의 CoroutineScope의 생략에 대해 다룬 #6-4에서 이어지는 글이다. 이전 글에선 CoroutineScope을 생략하는 게..

[Android] Jetpack Compose - ViewModel에서 State 사용하기

#1 개요Jetpack Compose에서 ViewModel을 사용해본다. Jetpack Compose를 사용하지 않는 전통적인 방식에서의 ViewModel과 크게 다를 게 없다. Jetpack Compose에 ViewModel을 구현함으로써 State Hoisting 패턴을 극대화시키는 효과도 확인해본다. #2 코드#2-1 MainActivity.kt...class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Box( modifi..

[Android] Jetpack Compose - State Hoisting

#1 개요 상태를 호이스팅할 대상 위치  |  Jetpack Compose  |  Android Developers이 페이지는 Cloud Translation API를 통해 번역되었습니다. 상태를 호이스팅할 대상 위치 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Compose 애플리케이션에서developer.android.comState Hoisting(상태 호이스팅) 패턴을 적용해 코드의 잠재적 유지보수성을 높혀본다. #2 코드#2-1 State Hoisting 패턴이 적용되지 않은 코드...class MainActivity : ComponentActivity() {    override fun onCreate(savedInstanceState: Bundle?) {  ..

[Android] Jetpack Compose - State Remembering

#1 개요 상태 및 Jetpack Compose  |  Android Developers이 페이지는 Cloud Translation API를 통해 번역되었습니다. 상태 및 Jetpack Compose 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 앱의 상태는 시간이 지남에 따라developer.android.comState가 가지는 위치 제약 그리고 해당 제약을 해소하는 방법에 대해 살펴본다. #2 코드#2-1 에러가 발생하는 코드 (State object의 위치 제약)@Composablefun ButtonExample(modifierParam: Modifier = Modifier) {    val count = mutableStateOf(0)    Button(     ..