Kotlin

    [Kotlin] Flow<List<T>> 에서 List<T>로 변환하기.

    [Kotlin] Flow<List<T>> 에서 List<T>로 변환하기.

    간혹 코루틴에서 Flow를 사용하여 데이터의 흐름을 제어하는 경우에 Flow 내부에 List를 사용하는 경우가 종종 있다. 이러한 상황에서 Flow 내부 List를 얻기 위해서는 flat-mapping 을 사용하여 얻을 수 있다. suspend fun Flow.flattenToList() = flatMapConcat { it.asFlow() }.toList() suspend fun test() { val flowOfLists: Flow = flowOf(listOf(1, 2), listOf(3, 4)) val flatList: List = flowOfLists.flattenToList() println(flatList) }

    [Kotlin/Plugin] Json으로 data class 추출하기

    [Kotlin/Plugin] Json으로 data class 추출하기

    개발을 하다보면 Json 파일 정보를 이용하여 매핑할 수 있는 클래스를 만드는 일이 간혹 있습니다. 간단한 Json 파일 형태라면 수작업으로 빠르게 완성할 수 있겠지만 Json 파일 포맷이 매우 큰 경우, 그리고 복잡한 구조인 경우에는 클래스로 정의하기 귀찮고, 복잡하며, 매우 하기 싫은 일 일수 있습니다. 이런 귀찮음과 불편함은 누군가 해결해주었고, 그 해결법중 안드로이드 스튜디오 플러그인에서 설치하여 간단하게 사용하는 방법을 공유드리려 합니다. 추천하는 플러그인은 JsonToKotlinClass 입니다. https://github.com/wuseal/JsonToKotlinClass GitHub - wuseal/JsonToKotlinClass: 🚀 Plugin for Android Studio And ..

    [Kotlin] @Volatile 키워드란?

    아래 코드는 Android developer 페이지의 Codelabs에서 제공하는 예시 코드이다. 해당 코드를 보면 Singleton 구현 시 변수에 @Volatile Annotation을 붙히는 것을 볼 수 있다. 이게 어떤의미 일까? // Annotates class to be a Room Database with a table (entity) of the Word class @Database(entities = arrayOf(Word::class), version = 1, exportSchema = false) public abstract class WordRoomDatabase : RoomDatabase() { abstract fun wordDao(): WordDao companion object..