728x90

Coroutine 사용방법

  1. Dispatcher(=작업 종류) 결정
  2. ispatcher로 CoroutineScope 생성
  3. 3. CoroutineScope의 launch/async에 실행할 코드 블록을 넘김

 

CoroutineScope

- 코루틴의 실행 범위, 코루틴 블록을 제어할 수 있는 단위

 

  1. GlobalScope
    • CoroutineScope의 한 종류. 최초 접근시 만들어짐.
    • 앱이 동작하는 동안 별도의 생명 주기를 관리하지 않고 사용할 수 있음.
    • 이는 안드로이드 앱이 처음 시작부터 종료 할때까지 하나의 CoroutineContext 안에서 동작하도록 할 수 있음.
    • 다만, Job을 컨트롤하기에 접합하지 않음에 주의 해야함
  2. Lifecycle-Aware Coroutine Scopes: 
    • Android Lifecycle용(KTX 종속)
      1.  ViewModelScope
        • androidx.lifecycle ViewModelKt .class public val ViewModel.viewModelScope: CoroutineScope CoroutineScope tied to this ViewModel. This scope will be canceled when ViewModel will be cleared, i.e ViewModel.onCleared is called This scope is bound to Dispatchers.Main.immediate
        • ViewModelScope는 앱의 각 ViewModel을 대상으로 정의됩니다. 이 범위에서 시작된 모든 코루틴은 ViewModel이 삭제되면 자동으로 취소됩니다. 코루틴은 ViewModel이 활성 상태인 경우에만 실행해야 할 작업이 있을 때 유용합니다. 예를 들어 레이아웃의 일부 데이터를 계산한다면 작업의 범위를 ViewModel로 지정하여 ViewModel을 삭제하면 리소스를 소모하지 않도록 작업이 자동으로 취소됩니다.
      2. LifecycleScope
        • LifecycleScope는 각 Lifecycle 객체에서 정의됩니다. 이 범위에서 시작된 코루틴은 Lifecycle이 끝날 때 취소됩니다. lifecycle.coroutineScope 또는 lifecycleOwner.lifecycleScope 속성을 통해 Lifecycle의 CoroutineScope에 액세스할 수 있습니다.

 

Reference

thdev.tech/kotlin/2019/04/05/Init-Coroutines/

developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope

zion830.tistory.com/56

 

728x90
728x90

Kotlin의 Delegates 사용

 

  1. Observable
    • 갱신 거부 불가능, 데이터를 넣어주는대로 갱신함
  2. vetoable
    • 갱신 거부 가능
    • boolean을 반환: true-데이터 갱신/false-데이터 비갱신

 

 

Reference

medium.com/androiddevelopers/built-in-delegates-4811947e781f

medium.com/@yeongpal/kotlin-%EC%9D%98-delegate%EA%B8%B0%EB%8A%A5-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-b358211a756

 

728x90
728x90

Android Studio에서 corotine을 쓸때

 

 

Update

In the recent releases of Kotlin Coroutines, I notice if a method doesn't contain any coroutines code(like launch, async, etc), the compiler complains This inspection reports a suspend modifier as redundant if no other suspend functions are called inside. So I assume that suspend should be only used when it's a must.

Update2

An advice from Google

 

Reference

stackoverflow.com/questions/54554753/when-to-use-kotlin-suspend-keyword

728x90

'Dev > Kotlin' 카테고리의 다른 글

[Kotlin/Coroutine] CoroutineScope 종류  (0) 2020.11.19
[Kotlin] Built-in Delegates  (0) 2020.11.19
[Kotlin 기초] Boolean Array 일괄 초기화 방법  (0) 2020.10.26
[Kotlin] 증감 연산 함수  (0) 2020.09.22
[Kotlin] @JvmField, @JvmStatic  (0) 2020.08.20
728x90

 

fun main() {
    println("Hello World")
    
    
    var booleanArray = Array(5){ i -> false}
    
    booleanArray.forEach{
    	println("booleanArray:" + it)
    }
}

 

결과는 아래와 같다

Hello World
booleanArray:false
booleanArray:false
booleanArray:false
booleanArray:false
booleanArray:false
728x90

'Dev > Kotlin' 카테고리의 다른 글

[Kotlin] Built-in Delegates  (0) 2020.11.19
[Android/Kotlin] Coroutine - suspend가 redundant?  (0) 2020.11.05
[Kotlin] 증감 연산 함수  (0) 2020.09.22
[Kotlin] @JvmField, @JvmStatic  (0) 2020.08.20
[Kotlin] null 가능성, lateinit, lazy  (0) 2020.08.19

+ Recent posts