[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
}
}
- 이전글[Anko] Anko Commons 20.05.30
- 다음글[k] POST방식 - Kotlin 에서 웹서버 Json 문자열 받아 dataModel List 로 받음 19.11.24
댓글목록
등록된 댓글이 없습니다.