일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- DataBinding
- ViewModel
- notification
- Behavior
- Android
- Algorithm
- AppBarLayout
- CoordinatorLayout
- 알고리즘
- 안드로이드
- View
- recyclerview
- CustomView
- onMeasure
- activity
- Navigation
- Coroutine
- 백준
- sqlite
- lifecycle
- CollapsingToolbarLayout
- kotlin
- hilt
- 코틀린
- 알림
- room
- BOJ
- HTTP
- LiveData
- onLayout
- Today
- Total
목록Kotlin (코틀린) (38)
개발일지
Gson Json을 Serialize, Deserialize를 돕는 라이브러리입니다. Gson을 통해 Object와 Json을 쉽게 직렬화, 역직렬화 할 수 있고 다양한 옵션을 제공하기 때문에 상황에 맞게 Strategy를 설정할 수 있습니다. Class data class LoginForm( @SerializedName(value = "id", alternate = ["clientId", "userId"]) val id: String? = null, @SerializedName(value = "password", alternate = ["clientPassword", "userPassword"]) val password: String? = null ) data class Profile( val name..
drop N개의 숫자만큼 앞에서부터 버리고 나머지를 리턴한다. fun main() { val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val drop = list.drop(5) println(list) println(drop) } [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [6, 7, 8, 9, 10] dropLast N개의 숫자만큼 뒤에서부터 버리고 나머지를 리턴한다. fun main() { val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val drop = list.dropLast(5) println(list) println(drop) } [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] [1, 2, ..
any() Collection에 하나라도 있으면 true => 만족하는 하나를 찾을때까지 검사한다. fun main() { val list0 = listOf() println(list0.any()) val list1 = listOf(1, 2, 3) println(list1.any()) val list2 = listOf(1, 2, 3, 1, 2, 3) list2.any { println(it) it == 3 } } false true 1 2 3 all() 모든 원소가 일치하면 true => 전부다 검사하거나, 중간에 일치하지 않는 원소가 있으면 끝난다. fun main() { val list0 = listOf() println(list0.all { it > 0 }) val list1 = listOf(1, ..
Mutable vs Immutable Mutable : 내부의 값을 변경할 수 있다. Immutable : 내부의 값을 변경할 수 없다. -> mutable로 선언하려면 앞에 mutable을 붙이면 된다. (생략할 시 immutable) fun main() { val immutableList = listOf(1, 2, 3) // ERROR // immutableList.add(1) // immutableList.set(0, -1) val mutableList = mutableListOf(1, 2, 3) mutableList.add(1) mutableList[0] = -1 println("Immutable : $immutableList") println("Mutable : $mutableList") } I..
Coroutine에 접근하기 위해서는 CoroutineScope를 사용해서 접근해야 한다. CoroutineScope는 interface형식으로 정의되어 있고, CoroutineContext를 사용해서 생성하거나, 미리 정의된 GlobalScope를 사용할 수 있다. GlobalScope GlobalScope는 별도의 생명주기가 없고, 실행되면 작업이 종료되거나 앱이 종료될 때까지 남아있는다. fun main() = runBlocking { GlobalScope.launch { println("Running on GlobalScope ${Thread.currentThread().name}") } delay(1000L) } Running on GlobalScope DefaultDispatcher-worke..
suspend함수는 순차적으로 실행되는게 기본이다. fun main() = runBlocking { val time = measureTimeMillis { val one = doSomethingOne() val two = doSomethingTwo() println("Result : ${one + two}") } println("Time : $time") } suspend fun doSomethingOne(): Int { delay(1000L) // 무거운 작업 return 10 // 결과 리턴 } suspend fun doSomethingTwo(): Int { delay(1000L) // 무거운 작업 return 20 // 결과 리턴 } Result : 30 Time : 2025 async 하지만 do..
cancel() Job을 취소시키는 함수, Job이 취소되었을 때 suspend 함수를 호출하면 JobCancellationException을 발생시킨다. fun main() = runBlocking { val job = launch { repeat(5) { try { delay(1000L) println("Job Run Index($it)") } catch (e: Exception) { e.printStackTrace() } } } delay(3000L) job.cancel() job.join() } Job Run Index(0) Job Run Index(1) kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled; ..
코루틴 빌더 runBlocking { } -> 해당 코루틴이 끝날 때 까지 Blocking한다. fun main() { // delay(1000L)가 실행되는 동안 main함수가 끝나고 println을 만나지 못하고 종료 GlobalScope.launch { delay(1000L) println("Pass") } } Process finished with exit code 0 fun main() { GlobalScope.launch { delay(1000L) println("Pass") } runBlocking { // delay(2000L)이 끝날 때 까지 Blocking 된다. delay(2000L) } } Pass Process finished with exit code 0 launch { } ->..