본문 바로가기

안드로이드

[코루틴] 코루틴의 동작 방식 코루틴 동작 방식코루틴은 일시 중단이 가능하고 필요한 시점에 다시 실행을 재개할 수 있는 기능을 가진다일시 중단과 재개를 위해 코루틴의 실행 정보 저장이 필요 Continuation Passing Style (CPS)continuation(이어서 실행해야 하는 작업)을 전달하는 스타일의 방식함수는 결과를 직접 반환하는 대신, 결과를 처리할 다음 단계(continuation)를 인자로 받고실행이 완료되면 continuation을 호출하여 다음 단계 시작// CPS 스타일fun CpsFunction(arg, (result) -> { /* next 작업 */ }) // 일반 함수fun generalFunction(arg) // compile 시 cps 스타일로 변경되는 suspendsuspend fun doS.. 더보기
[코루틴] 무제한 디스패처 (Unconfined Dispatcher) 무제한 디스패처 (Unconfined Dispatcher)코루틴을 실행시킨 스레드에서 즉시 실행하도록 만드는 디스패처호출된 스레드가 무엇인지 상관없어 실행 스레드가 제한되지 않아 무제한 디스패처fun main() = runBlocking { launch(Dispatchers.Unconfined) { println("launch 실행 스레드 : ${Thread.currentThread().name}") }}// 결과launch 실행 스레드 : main  특징코루틴을 생성한 스레드에서 즉시 실행fun main() = runBlocking(Dispatchers.IO) { println("runBlocking 실행 스레드 : ${Thread.currentThread().name}").. 더보기
[코루틴] 코루틴(Coroutine)과 서브루틴(Subroutine) Routine (루틴)특정한 일을 하기 위한 일련의 명령(함수) Subroutine (서브루틴)함수 내에서 실행되는 다른 함수 루틴과 서브루틴fun main() = runBlocking { mainRoutine() // main의 서브루틴}fun mainRoutine() { mainSubRoutine1() // mainRoutine의 서브루틴 mainSubRoutine2() // mainRoutine의 서브루틴}fun mainSubRoutine1() { ... }fun mainSubRoutine2() { ... } 서브루틴 특징한 번 호출되면 끝까지 실행 서브루틴 실행 시 스레드가 사용되어 서브루틴 종료시까지 루틴은 다른 작업 X Coroutine (코루틴)함께(Co) 실행되는 루틴.. 더보기
[코루틴] 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 코.. 더보기
[안드로이드] 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.. 더보기