코루틴으로 비동기 코드를 동기 코드처럼 작성하기
·
Kotlin
도입: 나는 코루틴을 잘 쓰고 있는 걸까?나름 안드로이드 개발자로서 프로젝트 곳곳에 코루틴을 적극적으로 활용하고 있었다. CoroutineScope 와 launch, suspend 키워드를 익숙하게 사용하며, 비동기 처리를 잘 구현하고 있다고 생각했다. 그러다가 문득 프로젝트의 코드를 다시 보며 이런 생각이 들었다."코루틴을 사용하면 콜백지옥을 해결할 수 있다는데, 왜 난 아직도 콜백 방식으로 구현하고 있지?" 일시 중단 함수인 suspend 함수는 함수 내부에 일시 중단 지점을 포함할 수 있어, 비동기 작업을 마치 순차적인 동기 코드처럼 직관적으로 작성할 수 있다는 장점이 있다. 하지만 나는 그동안 기존의 습관대로 코드를 작성하며 코루틴의 장점을 놓치고 있었다. 코루틴 스터디에서 스터디원들과 이야기하며..
코루틴은 왜 경량 스레드라고 불릴까?
·
Kotlin
Intro"코루틴은 Lightweight Thread(경량 스레드)다." 코루틴을 설명하는 가장 흔한 문장이다. '그래서 정확히 뭐가 가벼운데?' 라고 묻는다면 자신 있게 대답할 수 있을까?(코루틴이 뭔가요? 경량 스레드요. 그게 뭔데요?) 나는 보통 이렇게 대답하곤 했다.코루틴은 스레드보다 생성 비용이 적고, 컨텍스트 스위칭이 빠르다.또한, 하나의 스레드 위에서 스레드를 블로킹하지 않고 여러 작업을 수행할 수 있다. 이것이 바로 코루틴이 경량 스레드라고 불리는 이유이긴 한데.. 말로만 이해하려니 와닿지가 않는다.'왜' 생성 비용이 적다는 것이고, '어떻게' 스레드보다 전환이 빠르다는 걸까? 생성 비용과 전환 비용, 그리고 작동 방식.이 세 가지 관점에서 코루틴과 스레드를 직접 비교하며, 코루틴이 '경량..
Retrofit에서 Ktor로: 안드로이드 HTTP 클라이언트 마이그레이션
·
Android
Intro현재 진행 중인 안드로이드 프로젝트를 KMP(Kotlin Multiplatform)로 전환하기로 결정하면서, Android 의존성을 가진 라이브러리를 멀티플랫폼 환경에서 작동하는 라이브러리로 교체해야 했다. 그중에서도 HTTP 클라이언트를 Retrofit에서 Ktor로 마이그레이션한 과정을 작성하려고 한다. Retrofit은 OkHttp 위에 구축된 고수준 추상화 라이브러리이다. 인터페이스만 정의하면 구현체를 자동으로 만들어주는 편리함 때문에, KMP를 고려하지 않았던 프로젝트 초기에는 당연한 선택지였다. Ktor은 JetBrains에서 개발한 Kotlin과 Coroutines 기반의 비동기 프레임워크로, 멀티플랫폼 네트워킹을 지원한다.API 요청을 위해 Retrofit은 인터페이스만 만들면 되..
[Android] Safely Refreshing Tokens with Authenticator and Mutex
·
Android
When building an app with JWT-based authentication, handling Access Token refresh is essential.In particular, when multiple APIs are called at the same time, unexpected concurrency issues may arise during the refresh process. In this post, I’d like to share the troubleshooting experience I faced while working on my project.JWT Token SystemJWT authentication typically involves two types of tokens..
[Android] Improving BottomNavigation Fragment Switching Performance
·
Android
When implementing BottomNavigation in Android, one common decision is whether to switch fragments using replace or add + show/hide.This post documents my experience and reasoning when moving from the replace approach to add/show/hide for better performance and state preservation.The Original replace ApproachsupportFragmentManager.commit { setReorderingAllowed(true) replace(binding.fcvFragm..
[Android] When Exactly Is onPause() Called?
·
Android
Every time I study the Android activity lifecycle, the one callback that always feels vague and confusing is onPause().While I understand the theory behind it, I often get confused about when exactly this method gets called during actual development. What the Official Documentation SaysAccording to the Android official documentation:The system calls this method as the first indication that the u..