[K] coroutine > Kotlin/Java/Android

본문 바로가기
사이트 내 전체검색

Kotlin/Java/Android

[K] coroutine

페이지 정보

작성자 sbLAB 댓글 0건 조회 2,759회 작성일 19-11-25 19:51

본문


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
}
}

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
86
어제
513
최대
1,279
전체
222,649

그누보드5
Copyright © sebom.com All rights reserved.