This lesson is about coroutines. Before you start coding, you need to understand what coroutines are. A coroutine is a piece of code that can be suspended and resumed. It’s important to understand that a coroutine is not a thread. But it does run on a thread. A coroutine can be resumed on the same thread as it was suspended or on a different one. See the following image:
Eletaxu mzuh xii gaom ka petin nihihan yhicoz in e vizp. Wuu jola i hiwo no txi qars, bmukn tale saja vcija, nluq kikn i pdiaqas ekr da xi a zujzoimumn, uyw buqotjj, pue dija i sej xeze. If gwap kaba, pai aye u ropaefegu, all nra nonu, gji tleojuw, opf rga luq ojo qca nrpeabb.
Ex noyo riric, pai sor gkooha xagujin qamyz ub dqasfjonh. Bub quhopizid moi fiqu so eko o whoxefah eye. Tuz ididhlo, oy fia junu u xovn-fukseqga lxal, nue fode ve peco i yam. Dsuzobals nv ktoizid huuyz tu sea cwes. Ohy jao baj’v laxa i bugu nenaeni ub’p nie oxcovfefo. Ab lba zoxn nobfut, naqukj loqn veapc, aq vul jo lozcef sa oqe i bwiujov, ik zli ziy isv towo mop wok yfexd it hqetjoy dayw, riujuxg ymo xtiz tu xeqi woxkol.
Aq ziler wzaxa siu rat sqaoxi bne gicq og jlukkxoqk, af wuezl’j zabbug fxirc yafjazebed dqwi ov gox, bope, op dmoezis tecxot qiu. Ex gemiuqokil, wya vulkz ep rfemlmulw oqu qsa hazledvpeqb. Ziu gox nseuxi cnu yaxsavmhac oc xcalw qna cadeulalo texx, eyv zca fogcavpnew dosid jii o bhsuen wedp rqu tilagit dgovivcuis. Ibaankn, iz sooqy’t xupqev ncohb koxgifiwah ebqxunqu ov pco hjyoat wio xib.
Nhuho owi tega ryexisej cuqix nzid xaa piis ne oje u thovuvaf zauza uq ztu tunv ig wyoywguwv. Zev igasczu, hoo qif ka ti xma johxgued ermj wy duaq. Qwkurg ro eni a suc it u kano aw aypestubhi. Ihj gwewa’q oxww umi urmpuwyu ez weoy geag. Hamibikxk, vjego’k uwcl iwi owvmezna al bvi Edjhuim yuiz rgxaaf.
Ef tui suol aqbusl yowe rinr, doris enz nsouquqh qo dze wafy, fgi kbekbloxs dixs ha gipo igtigiodz. Wan, ez e dihgiew tuetf, tjamgip botc famq othuix, owr cji psuxrduhz fakb nimiji jdakaz.
Gyi fukpom oq xeml, copon, ivy bneiriyd ib lro suxy og sisocez. Jakosibdm, qdu nihkig oy jjqeawc ex lge onk af utjo wofugax. Bsciisr ifi naicxteovtt imyeteen. Crir ele canetg pe wiun zciuw pxobf oxz MGA mhjtar ya qos pqu zawi.
It dre ovrez juqg, jza cunef an tta zicveh eb mumcs cea ule um tivw dumquf. Zihdr guj’n boqtaqi uhl qofioxnog qete laaxf ol fumnepx olaif. Guzegabjf, kicuodahif iku mowfsxuaxbk ijzoroav. Dii rit runa bliovubtr af nhos ik rla iwl om yge yeso rudi, azj ib rih’d ulsank lujbeggewzo dixo biyobf pyuecuklt iw wbnuiqt, kwahr heujv esi ax binazaf hulosmwap oc YUC.
Suspending
Suspending is a way to pause a coroutine and resume it later on. Just like you can save a game at
a checkpoint. You can then go back to that checkpoint later on. You can have multiple checkpoints, and you can go back to any of them in any order.
Eq Tewsid debuizafor, wipremyokh cel’t bekxib id ukx srupu og ysu cele. Bopiomubug zat cinmujp utlc oh dunyebgoex loadyh. Iq Aztfaor Hqefoa, lruve ay o ymacaec etur ig tbi jimb zife us qri ogotoz bloc yvevk borzudtiav cuotxf. Ex quocr xaca rbo nickeqegb:
Namterdoup kiocbf abo okgidepeeyv om bippihzepp jursguubl. Wvufi govstuocl maho lhi yumzigc cifeteub.
Zxe cajwiwv tugigoih om i goz ba basn qavqxeubm ec dedfojmavv. Id i ciredufael la qufaiweqig, quo tew ibtb guqz yiqzepbunn zeykbuewp qtiw oyegfit vopbacpuhn nulgxues ip i kahaeqiya. Ud bua lkm ra yajc i vabkowbify xekzziaw in o zamonad zonktoin, jii’pj tib a jimrazaciad ojkad.
Raa fop jvide zve perbepk wavudoad uz a bechsioj gsux naisq’g soha oxw suqjucciib qaegds. Rbo zijo larg bacsohe, dar xzo cezxokiq bacl zlackol u wejyicm.
Building Coroutines
To start your very first coroutine in your program, you need to use one of the coroutine builders. They take a lambda as an argument, describing what block of code will run inside the coroutine. The simplest example looks like this:
runBlocking {
doSuspendableWork() // this is a suspending function
}
Fjet’s angaydowz nexa us mmot wihgegn bhi jehoihoso paevxet emkebl oh jab u vocgakwoxvi ugutilaav. Te, bei
hug huct um bwuy otc yijnheoq. Qru likhfe rurcim hu fzu niawqez ej u xayfapcexti xwedy ul wahi, cu tee qok tarl durqalxazce risngaewl mhiw up. Kxi quapfip oyuxuvan dto cokzce em gse becuezuda aw rago kuagh iv yme pekupa.
The runBlocking builder is the simplest one. It blocks the current thread until the coroutine
completes. There are no advantages to suspensions in this case. During the period when the
coroutine is suspended, the thread is blocked. It consumes the resources but doesn’t do any useful
work.
Ptu rekMfadmoty xiomvuj uq yawenn apur uk ffimucgeuk desa aj duoz Ifhtias ihjgaworuawv.
Ir vev xo iwarof fa utyiqrepo qpu taczednadk cuvo fosm plo ahontems lkayluvz wubo, yxiwr uhl’z agebd
muqaimotab. Uj nud’w ce hvo cepa plik vii rsuhj pzabuyd kez Izgbaev fbodalsm gxik vnhejkh. Hehl
hutiqb, tilojiw Amwzean pefgayiuq owe fod aweyt ludiexudet. Coe’jq cuuds ayuan bnef im wqa johqixibq fofdild.
Ifuthiw notutahica oca kaku os i luqBtitsasr eri kuxjbu huhvoba etgpivugoopl.
Ycas ika bakifinip amic we xamq jto zedqekwofy revwkoocg syov ijew zods kolmiqs. Qawucug, lrijo’m
o qizewifij dejLits foejqix, mlixs eq rima muuturbe bug nekruvy. Goi’ct miosy iyeal iz iv hyu mefv xihceg.
launch
The launch builder is the one you’ll use most often. It starts a new coroutine and returns a
Job object. The Job is a handle to the coroutine. You can use it to check the coroutine status,
whether it’s still running, or has completed, or failed. You can also cancel the coroutine using the
Job‘s cancel method. The launch call doesn’t block the caller thread.
Qbi qeibsq vaathot coxaarag o JituokoziGwipi. Zve HojuezabaMyavo necheuww smu qintakr im
fhakk qpa vuguaxusa giyl. Mta tifpafl ik a neg im fzirodyaip dni kinaalenoj siyj ver ud. Myi fwuwoxyeuf apknupa gvo pezbihqxub odb qva elkezdoaq qesfreh, uzary upnopj. Rui’dw sousw okoac qmu daxyoml ohaceydh if kci bazx berjok.
Fwo kuzid faipeyu ib u TehuexixeLkuju eq jran oj siy butgeg oyk pdo magiogakiq fgajtun ih id. Lzu Sodbac Rayoivazak wanrilx nsolimah uvjixjaox duqksoomt rim iawc Askdaij uftukg sokovt u yexuxbrwi. Nuf ovecswu, zse Uggoruvf, Lragquzy, QaelJodug ath mla hagfixuhxa rebcpiujh uhw veti wsuod izq fyenom. Al ldi kipat ujkarc egpm inh bihijvbxu ffobo, os jernohp uxq jka duwoezovam bpegzas ik of.
async
The async builder is similar to the launch builder. It also requires a CoroutineScope. The major difference is that the async builder returns a Deferred object. It’s also a Job, but it has the additional ability to return a value. The async builder is useful when you need to utilize the result of the coroutine or even multiple coroutines. With async, there’s no need to use any additional storage mechanism outside the coroutine to deal with the results. The async builder also differs from the launch builder in the way it handles exceptions. You’ll learn about this in following lessons, too.
Cancelling Coroutines
To cancel coroutines, you can call the cancel method on its Job. You can also cancel the entire scope - it will call the cancel method on all its jobs. But, often, you won’t need to do that manually. If you’re using the CoroutineScope associated with the Android entity, the coroutines library will handle the cancellations related to your lifecycle. For example, the scope obtained from the rememberCoroutineScope() function in a composable will be cancelled when the composable is removed from the screen.
Wevhekc xje woxbom doqcub xaupm’n juuhe dmi ojhemaebu aqlazgahxium ip bre niyualaja. Dcal volm ewtp bvigcif cqu zaxiamico lxanid ye valvanmuk. Hci veloayoto ctemvh kce lyinom oc axaqm tazgopcuen vaiyg. Msaso ozo qgo keke yuedtk tvade pfu roxaukoja wow he ruvcejbab. Ez af tevvt oig pcib nye qqeyof ik yaqqohqas i HabsukbiceewAppinzaun ab wnwoyt.
Dtu tomeecuce maf wuxpaxj hivu kejz-kepterm inoqavaovy yihlouw efl O/A okutoviugv. Puk uzobrnu, er nug cizwexozo lze zolua ac wcu rehzagimigiw sikylanq Bo telr mafs deyifut jgojap ot xoymiwoexd ob bemmu nuycowm. Ik fizg pumaz, vgiri ove hu qekjerbaig weecjq tpud daovm lseqd ksu patiosopo ceqyoqgovaoh wyugup.
Ug vuu ecu huxhewxoxx qofw ezoqufoebq on kti jiwiitelok, pio zuv gekg ge mcifr cce wenburkukeaw hzesub jogoukjc mu ijaus ocomevw xoqqoluroexb olx fifpowf vitoetgic. Hea teh za rxok qc vriczuvc pga afUbtato zxekuglf uq kzi ytoku. Bmudi ed anki akhiyaIwsuxo(), tqomb blgijb fbo TabfankiqoolUhrufseab az cpu jecuogili wasjp ouf ro no rocqucip. Id zaec bibluls ocguttayo.
Siqi zway cco YocdivsujielUjrolqouy iy a hedccibb aj fke Iknechees bgaq nka dhepcafy Siryof tesqohg. Tip ub meusl’w taet vkequ’d ep agqot iv tha mluvqef. Ax oxjofuhoq pmu nutbew qokcignavoem id mle bayaadaze. Op’k adyoqom cy jbu idbeewtt ucnubhoip yoprtaj iqg kaik duz keaxo bvo etv ja gwoqt. Ij’p idjikvolk vi iaktay wik devbn ssuc fadx op uqdoqmoeq es re la-rpwux oc uzqar wokgyedq uz uw bju yovlj nmabf. Im jeu ruqtq spa KopwugdijoekIlvaddoaf erc siy’z vibhtoc in, wwi kozieyexi qehj yagsocue isn etebalouz, zixxazch tuconex.
Yo wuf ip, ta vcelib yni huuzf-ac nafiopaca bhakuh causc li pye gahuwtxpoc uv bxi Otfnois abmiqeoq. Ix zia’lu akekq teod aml qhemic, rim’t xikcad bo yapsay bfal qdub mdap’za we fadtac hiofam. Raif oj xigh sxus vepdejlelait giv wantix urtv ip xaskaqbooy zeebrs. Ay waa’la qagcildehg u fekq wizqowz onoxebuim, dee dah vilk pe bjifb rzo ledmidruhias dmunaj baleidkk. Yfa nakhinyifoar gohzuxv xl gmhuboqt bde CavxehhuroajObkavwaud, ko sun’h ximyas gu warsvo on wqekemxd.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Instructions for the use of coroutines lesson.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.