본문 바로가기

전체 글

[코루틴] 구조화된 동시성 구조화된 동시성 (Structured Concurrency)비동기 작업을 구조화함으로써 프로그래밍을 안정적이고 예측할 수 있게 만드는 원칙  코루틴의 구조화된 동시성 특성Parent - Child 관계fun main() = runBlocking { launch { // 부모 코루틴 launch { } // 자식 코루틴 }}부모 코루틴의 실행 환경(Context)가 자식 코루틴에게 상속Job을 제어하는데 사용부모 코루틴이 취소되면 자식 코루틴도 취소부모 코루틴은 자식 코루틴이 완료될 때까지 대기CoroutineScope을 사용해 코루틴이 실행되는 범위 제한 가능   1. 실행 환경 상속기본적으로 부모 코루틴이 자식 코루틴을 생성하면 Parent CoroutineContext ->.. 더보기
[코루틴] CoroutineContext CoroutineContext란?public fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Jobpublic fun CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred 코.. 더보기
[코루틴] async & Deferred async란?- 코루틴 빌더- launch 빌더와 비슷, async는 결과값을 담기 위해 Deferred를 반환// async. Deferred 반환public fun CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T): Deferred { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyDeferredCoroutine(newCont.. 더보기
CoroutineDispatcher 란 무엇인가 코틀린 코루틴의 정석 책을 참고하여 CoroutineDispatcher에 대해 알아보자. CoroutineDispatcher 란 무엇일까?Coroutine(코루틴) + Dispatcher(보내다)로 코루틴을 보내는 주체를 말한다. 그렇다면 코루틴을 어디에다 보내는 것일까? 스레드(Thread)로 보낸다. 즉, ThreadPool 안의 스레드에게 코루틴을 보내 실행시키는 역할을 담당한다. 코루틴 Dispatcher는 아래와 같이 CoroutineContext의 Element임을 알 수 있다. public abstract class CoroutineDispatcher : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterce.. 더보기
[안드로이드] RxJava3CallAdapterFactory 이번 포스팅에서는 RxJava3CallAdapterFactory에 대한 내용을 알아보고자 한다. 안드로이드에서 Http 통신을 할 때 대부분 Retrofit 라이브러리를 사용한다. 만약 Retrofit과 Rxjava3를 함께 사용한다면 한 번쯤은 이런 에러를 경험했을 수 있다. 아래와 같이 Retrofit 인스턴스와 UserApi 인터페이스를 정의했다고 가정해 보자. val retrofit = Retrofit.Builder() .baseUrl("https://openapi/base") .addConverterFactory(GsonConverterFactory.create(gson)) .build() interface UserApi { @GET("api/v1/users") fun getUser(): Sin.. 더보기
[안드로이드] Retrofit 분석 안드로이드 개발에서 필수적으로 알아야 하는 Retrofit에 대해 알아보자. 우선 Retrofit이 무엇인지 알아야 한다. Retrofit은 OkHttp를 기반으로 한 type-safe 한 HTTP client 오픈소스 라이브러리이다. 쉽게 말해서 안드로이드에서 클라이언트와 서버 간 HTTP 통신을 쉽게 해주는 라이브러리라고 생각하면 될 것 같다. Retrofit 내부 코드에 나와있는 설명을 보면 아래와 같이 나와있다. Retrofit adapts a Java interface to HTTP calls by using annotations on the declared methods to define how requests are made. Create instances using the builder a.. 더보기
[Android] Fragment ViewBinding 사용 시 주의할 점 Fragment에서 ViewBinding을 사용할 시 주의할 점에 대해 알아보자. ViewBinding을 사용할 때 Activity와 Fragment에서 binding을 생성하는 코드를 작성해야 한다. 하지만 프로젝트의 크기가 커질수록 Activity와 Fragment의 수가 많아지면 바인딩에 대한 boilerplate 코드가 생길 수 있다. 그래서 Base 코드들을 작성해 ViewBinding에 대한 코드를 적어놓기도 한다. View Binding을 Activity에서 사용할 때 abstract class BaseActivity( @LayoutRes val layoutRes: Int ) : AppCompatActivity() { protected lateinit var binding: T overrid.. 더보기
[Android] Fragment Lifecycle Fragment의 Lifecycle에 대해 알아보자. 안드로이드 개발을 하면서 수많은 Fragment들을 사용하게 된다. Fragment들은 모두 각각의 lifecycle을 가지고 있다. Fragment 클래스를 보면 LifecyclerOwner 인터페이스를 구현하는 것을 알 수 있다. public interface LifecycleOwner { // return the lifecycle of the provider. @NonNull Lifecycle getLifecycle(); } Fragment에서는 getLifecycle 메서드를 통해 Lifecycle을 알 수 있다. Lifecycle의 상태는 아래와 같다. public enum State { // Destroyed state for a Lifec.. 더보기