[K] coroutine > Kotlin

본문 바로가기

사이트 내 전체검색

Kotlin

[K] coroutine

작성일 19-11-25 19:51

페이지 정보

작성자sbLAB 조회 2,787회 댓글 0건

본문


coroutine 

fun getNetwork() {
CoroutineScope(Dispatchers.Main).launch {
// Show progress from UI thread
var data = ""
CoroutineScope(Dispatchers.Default).async {
// background thread
data = loadNetworkSomething()
}.await()
text_home.text = data
// UI data update from UI thread
// Hide Progress from UI thread
}
}

fun loadNetworkSomething():String{
return "20"
}

//launch-리턴값 없음
fun setup() {
val job = GlobalScope.launch(Dispatchers.Main) { // launch coroutine in the main thread
delay(2000)
val mProgressDialog =indeterminateProgressDialog(message = "Please wait a bit…", title = "Fetching data")
mProgressDialog.show()
for (i in 10 downTo 1) { // countdown from 10 to 1
text_home.text = "Countdown $i ..." // update text
delay(1000) // wait half a second
}
mProgressDialog.dismiss()
text_home.text = "Done!"
btn_home.visibility=View.INVISIBLE
}
btn_home.setOnClickListener {
job.cancel() // cancel coroutine on click
}
}

댓글목록

등록된 댓글이 없습니다.

Copyright © 소유하신 도메인. All rights reserved.
PC 버전으로 보기