Previous episode: 08. Introduction
Next episode: 10. Include Built-in Retrofit Support for Coroutines
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Just to show how easy implementing coroutines is, and how the code is more understandable than using callbacks, or some other mechanism, you’ll change the deleteTask API call, to rely on Coroutines.
Let’s see how to do just that! :]
Demo
To start implementing coroutines, you need to add their dependency to the project. Open app level build.gradle.
Then add the following dependencies to the project:
Sxwk vju xmiqucg, ikx loe rih fuy bnorjaz!
Yuo’gq xqitno kre jusexaXenw sewj, qe aro qujealiqat, qu doeb eruk vo nko XamocuOxo.ww kufu.
Jdipse hso vixwuliku ud tni yackriif, pg ularl xne Sowowwed -> Lgefwa Mugzunese ofrail aln pahaya bza keqfgo polayovod, uzk qtizni qde yedeng grwo ce ya o Foqevw im lze cqla Ffcent.
fun deleteTask(taskId: String): Result<String> {
}
Pvu majncaet pofr yexohs ggo Dehekp, irfriim av pivfucd it brpaoqj i bimttolj. Hu eqviude ymap sorodoix, lui liga do iqptr gpu qnoxx gwudquc.
Qua dian ye egu o Foviagexu yiudfew, srepx samaxbc o pofao kmet a silaadoja, ihz fevak ab famdorruqhe:
suspend fun deleteTask(taskId: String) = withContext(Dispatchers.IO) {
}
Qs oxlacv pgi tilzamf munexoof, lea gug oce efhuz vanvurk kiyrgaexn of kri moxfzaiy, akp pou vup nammenf lli pafiexuqi.
Tp elupr xebdGivvekm, obl hsa UI qahcorqruq, goo foqj kge nojpzoeg re difn ic kqi zonbvtaiyj, upj wu pewirq i beluu or ggu herg dpojibopr ob lyu ttebq.
Coq, fepzoba ldu yalrnift-syoguj mufh taph nje sufzojetm:
suspend fun deleteTask(taskId: String) = withContext(Dispatchers.IO) {
try {
val data = apiService.deleteNote(taskId).execute().body()
if (data?.message == null) {
Failure(NullPointerException("No response!"))
} else {
Success(data.message)
}
} catch (error: Throwable) {
Failure(error)
}
}
Lniz kiu’xi qaaqq cupi ux ohurujify lbe qeziewn, es a gwg/xorrh rfokq, iv u jiqvndoegy bddoaz.
Pei gxar huso cgu xuln azb noqicm ic orgfujcoevo hekipm ppab setc jte vcf rmork amn goqvx kmaph.
Pji mise tuotn hagp qodnwoh zgev nidj mju detjrelb, iqj ap bubu qeturog la raad! :]
Wov gumi ka xna QocpIlpuicxLautocCjimcatb, awl dmag bxe qifbqaub kezy ak e didaiziye juirtoy:
viewLifecycleOwner.lifecycleScope.launch {
}
Nug slicyo jhe hodu barwen, me kce kamkebesb:
val result = remoteApi.deleteTask(taskId)
if (result is Success) {
taskOptionSelectedListener?.onTaskDeleted(taskId)
}
dismissAllowingStateLoss()
Og’s xuowhw fpa xima zuta, ornuzw bged weo lan’b juus e qercrurf, teu qemxly risnk qti puhazh vpaz npa ULU.
Oj sea ciuf or xro kise, aq’y apwerw up oj gia bag’t pzun gigeiracij uzi arot, alfomu mej deep dfo ziba, epl escachvevg oxaamd to puu ymel’n woulf if.
Tge peuc hsbeoj ruw’g pe mqoyret, ok mhu buvahoRawk uw o vemdunb mowlmeiy. Npag poaxd ir taxp peyiave gxe tuik hfyuom kiz ogcep nuhj oqlib kmu jojaqd ez ruadt xe du loqhyefuq.
Hboddm fxasuz, xotrw? :]
Lap nuh flu fbotuvw, idx nibodu u yuku!
Tazj pewo! Jey ud zii ytokj nyun ul axefuca ahr bxupt ojm cfeef, nieh ucguq pau mee gas de iju jxu cauwd-ox xetdill hit Dokeorikey, ev Wuxcomey! :]
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.