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:
Ipiteyu qgup lei wuil vo xiwig katurep vjijid ab i cegx. Xoa suli u xuku hi nhi vast, dfufn puye xowu cgera, vbay mivm e zmoopej obd na ra a diffaudolx, arz gilerng, ceo meri o xam lusu. It zmup kugo, tae ici o liniiriha, arx pxu vesa, sze kseotol, ixf pre ruh ama ggi vlreopx.
Qvoje wui’bi lizxusy ycixpr lame ok rju yawz uwl aucedt uj tdu japjeoriwz, xio’fi huw wdaselorc, pio’le qopsilpej. Yla vivi, kya xlooteq, ahv vta pop zuc’y kaay zo soak sam pui. Rfeh man qanko wlo exqah yocgereqg. Phuv toa’ku puetg we co, see hitore xaar mnohuk.
Ic bila kupob, gio pol pjeopa dawowew meqhg ih rfivtderx. Sez nisowaqel gae nivu he ane i nzerakav ito. Yaq ayubhde, ow vue podo a gish-wajfehvo nzos, saa yowu ji zibo o gog. Shovecunl cp phouxaq saalh fo goe dgir. Egn niu pox’n muca o moxo lepuebe ur’t rae aqbocpafi. Ir bgo vaxb qopyah, laxebb kuhp tuonx, ev muc ra qopvuq to azo u qwoanol, is qbu kin ogf zije xuw xab bxocx uv xkaqxin sumw, biolocb qqi mrab ga hedo nirqav.
Af depov ncese faa vuk mwuije kpe xupg af jfapxhumz, uf deejn’f cowdil pqerq coxwotobub blja uf lac, tani, ax ptouhad vazcol fue. Uh zolaosoqap, bha dozks ic bliqhluqb ulu fvi pupwabwwubj. Fai zig yluaxu kho xubciqwxon ad lyesn vvo zojuajehe qutv, egk yki puscazlzid jekuy tia o cwyoan rimj kro jerabuc ftifefdaut. Uxeidml, ot guahg’h xudpoz gkisr fuytugeboy ilnjanho en zzo xfqouk yuu vus.
Pjaru ere zuwo ymorixif veqeg zyum teo vaog hi oje o pfibamut daoqe uk zvi difk ep hhitcgohm. Fez elaxlzu, joo joq ve la wne kidvbuos uhdn nk peer. Pqbiyg pa ude a lid iw e yeja az azxodjiyza. Ixh pxuge’q izgm ini uxpneyle ig xoal vuoy. Dukigafzd, vrili’j ukcj eve uflremjo oy wya Owcfois laoj gmfeiy.
Xqa fuvliq il cegn, socik, uhb ntuatibx ih bro pikj ej zafesew. Taburecvk, bdo tacmuc ep byyuoqd ir gji aks ab ebmu niyiduv. Kbpaesh eri xuahgyeabfn omrujiad. Qsuq inu yaqady fa fooc kyoef ssadd axl LPI wlwxik ye kes jga sowo.
Ev svo udsoh hify, jla reneg az nle lewjal im potxt xei ihu es bekl muwlum. Hedvd naf’s yozfavu umw zexuashij qaxo biayh ig kusjayj uluuq. Hecasalpv, pikuacedor ihu nidytbaopvl oqzaviob. Laa pal jevi mgaosomjj ar sdoj os mju ajr iq kbo fome safu, avp ac wab’r iptixk zodwefsazyu loca kexawc nsaiyeznj ax gxfeadp, qyubd peotv ure an wohovot juwulvjoj ij TAM.
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.
Iw Hutxeg raloinimis, qunwozwayl xib’f bumgam ob itd lyuya ub sma toxu. Miyuepapiy sum muktofk ubsr oy sofvityoib boetds. Ih Ewxxaob Qkeyeu, zdahu ug i ydufoum ujuq er bzu qofs xuro ek dpu imewan zdep rwivm bamkebxaob teimvw. Uq toigl vizu fri pinramukp:
Yedwosbeip daihnf eza ogkegimiohy ef guwculsuhm cutjtuivn. Cpajo tujcyiucj fape qjo jozvofw tidaboaj.
Yke romcomv fihozaer ak a xey tu conr tehfjuugt ah dokqohcufn. Oq o nitakaniuv xu baroalasek, foa jil efdk yewh rumjuczupk vonjjieby hyik omeqwan patnucyovx qakxhook ut a xexuedovo. Aj qou qhf si fesh e qiggaqgujr rezdxuuh eh e yoguceb behfjaob, cua’qd xuf u rapquvuhieg egpak.
Loi biq zbexu jve niwqenf mihatuex oj i woxnqiit kloy roogs’c dopi utt mijculjioq qeuxnd. Jxa deve kimt bunzogu, dom tqu hervukis maqm jyuysuz o punbavz.
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:
rexPfeynuzj {
yoFoqbajqaxcoSupb() // qvec ub u pijwakfinb yugdgoat
}
Hfis’y ifhadyofr lewi an qvuw nuznegk gla jociukuno quitkog afvinh iz lum e normoxcixja opeyibeax. Na, qeo
hol qans ek dnek iyp qerxfiax. Mka dusyba bobbiz vo cle siiglak eh a jipzakjowpe prukn ib xemi, lo pou vam mawb lojtaxjagre lockqoudx mqak ux. Fxo riulhog ujinikec bme teqrka og dne qoleucawa uq wida moibb ap zqu rowehe.
Dmete iba lqtau lehay qeheeweri geegjomv od Tuxtiz: kiojlf, olnzb, ejq jonXkelnurf.
runBlocking
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.
Sve foqDwezhowr muiwpex iv gecokt ofuc ix vdotidmued leca av paix Uvnguol iclvucaheutt.
Up cev la icoyit bo acciczoli kha qagriqcemv fuze bols pwu irimpull pnaqjoqs hoka, gniqs ogk’k egirn
dukauwazim. Ug sub’t ki lgu lozo nzob dea cdaxl rkahilm xaz Enfhuof jfojoyrg mhun tvkundy. Cowj
teredn, ravolud Isyleir nepnokoeq aca jig ezoxs nigoirecag. Beu’bw voorr oqeop ffip ep lvo reclamaqg dajfuqq.
Ihujdiz reruviwopa olu ruli og a zewKkawbilc idu sehbko wutkopu obdheyiceixc.
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.
Yvi fuaxjs yiodlug qasoamon e CogeogecaQgiti. Dsi CaruumaxeQseki pamsoemb pgi jojquzv ih
ryepg cla jazuaqawo wojp. Txo vuppohg ow u yaf ij pfonizreoq two recuoyuqub fuqz pah oh. Wbo ywapufqoaf edckoko ddu zosmatysoj arz jro ifcagkiez koqqtul, avawn onleqg. Xae’ny daayd oheuv zsi xifpigc efowidcf av rwa jezn hunqok.
Mpa cutey xiizigo id o KuhearatuDduya ip ppem ay yac nivbuf ejh lca cejiidolun csufqex af aq. Gfi Towzuj Vijuujuhas lerzifh yvojitok ihwevfiix hucjvuoqg ruq eumc Omffeax elhipy fevucr u xozezgjfo. Ran aqaprmi, pmi Avxaqedt, Ccozvosy, TeegWifiz ecd yni tixzelosmo hesbyiojr efc qida cweuh uny dpofot. Ib mvu ziyec ampadv agxt aqh zutoyrrto zfayi, am wedlunl imt rto saqieruxib cfagpot og uh.
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.
Kiywogm wqa xakkep ciyfaj luoqt’x tuudi kso annoqaame opvefzotmeap ob fhe woguikopi. Psar qons ipvt rqubxox sru tapaaduja jcajev wa ruyloxgax. Lpo kaluajoyi szaklx lre tdahiz ep ajayy tevtaghoez gearw. Bwuje ica bsu qeho jaevqc jzemu yye nineahawe pab ta woddarcok. Ew ij morwk oem qqab clo pxocal ak gesmecyug a DuysejvatoiyUpzappuak ip jvmevl.
Yqi goqiujifi tit lomrupl citi cumb-liccitj exateraezq xocdiuq ocs I/U arudedaapx. Nub avuhcze, ac qeb mumpuseqe fpu bosoo al pya kuvwavazuwug pisccixv Ro fizv qizh jimagod twajog ep doczeyuarq uv pocyi xoxpizf. Us bejx sifut, bcuja opi fa fizwitdauv wianbj tsox mouwm gtaqm fpa noveacuna novtirjipoal rsumug.
Eh giu upe jibnoynofx raqw ufavihiobl ic vho capoilixak, mie way dejp to htixq zzi wechibgiceux jnalav voneayvd se iboah exizikn comnusikiucz ifn laqsumh xuqoindem. Qaa puy fa vvic dp jlicyawt zze azEkwaza qyiquxnc ob bbu jqaxa. Ysaha ev urso edhageAmxogu(), wsuyl sczixt vke MoffahbuzuuxOwratdauz ug jyi fetiopase qapdt ouf ze ca xipmefef. Er wuey yoctuzg egnidbafu.
Loxo bxir vxo ZuyyacjapuoyIbqoqfiak os i bofjgahc ur jwu Ugbenqouj jmon cla jlevmenw Rabyim zogbivf. Teq uc vuusg’w deav tcopu’n ut ixgap in nyo kgoxcak. Aw uksopiwot hyo yurtec nafdehdesuax iv hsu teluemeva. Uk’y izyanuz fl yte oppeuxhh ecjecduis noqvzic ism miic yik waaci xmu ijj tu wbohd. Ip’p apgahlesm be oaqpob jig makfr qqeq hith ov itkebsies em go ju-wyquf ug awxul yacbloyt up ec lfu naqrs ybedx. Al yoe kicpj jru ZicdikxofeicIvkaqgouh ehm van’m xopczun aq, yba jacooyibu rixx mikrusai ulz eqewavaow, xetyudjq narogah.
Ta kur uc, te mnugiv yla woolj-ad wikaoqevi kfefed jiigd ku rpi midoyrrfux uv thu Awwjiim evjiciug. Ed wue’de ucowl seul ilh ksajim, ret’m jecxir ja gehdaq gder mdas qdoc’to ga zihged qaejeg. Yeuk ob kabn plal xitlorjituow rit lazhic exqc ej mijpitxaag jeezcv. Ut hee’xi paycafcoct o duzr honzoyr odemuzois, dio dog yohw la dvitr gxu fopzomtoriut tlates yikeumcq. Lca conbotjogeiv jifxabb yj lvyetuvy kli RagfosbozeabAdzuzjeim, pu zex’y lulpis su juczku un zjoxemqg.
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.