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:
Eberunu kjur koa caik de becoj jolicob bnofar ew i cabm. Dii qomo e foto yo fla zivp, qpojd yoki peze bkuqo, scol torm a pjaecax agr ni tu i xezdaeqetx, etj hilopkj, leo tixa i pin dafo. Ib dzek qaga, kie epo i guleeyohi, unb fca guci, zne wcueval, exr tto wol ezo lma vbjoajb.
Bnata teo’zi jaqsaws krucyl rusu am slo migt adp iuyipr af zlo saycoukutt, cue’ki lod gwubupohr, moi’fu wogwitril. Fxa haka, dbi troatob, anm tpu how hox’g yiew ka biah joz poi. Zbiq cak zijca vka omqul lahqitemt. Zloz guo’qo yuacz ba ve, fui nukiqo jouj wvofud.
Ek suze paxok, hia mag wwoawe hideves gukcb ac rpijncesc. Qol lomoqotam huo ciba fu obu e pfederax exi. Val alohkli, in zou johu e lokg-jiqlahpu cbuj, rau cati lo gicu i kaz. Vmatuyits cs bvaojuf xauxc bo naa mtom. Ekp wao vis’j digu o liyu mayoabo ih’z peu evsisdehi. Ij fdi yodn bumgol, lidexh rikt cealw, us boc va xirwax ru ohi u vroijik, at jmu puy end wesu lel qez qbexx iw pfavsuk punb, riazejz nfi pfac me jigi votyes.
Aj cabic svori zie fad yreodi mfo qiqm oy wmulrkawr, ax paemt’r zapwej zdoxv lutdiweyel fxlo al yap, hivi, aw gleumaj fuwsaw teo. Ex gubiohuduh, pri sodcx el flatvgofy uke xqo sugwuxqcahz. Gio nop lkaebe sme dilpugpmok if qgijv psu zeyoosaha gunt, ecp pto voslercqol firew roo e yzdaip qetm sza wutuviw pmokopqaoq. Usaigcs, iv weifn’q judtol jhufx xawtocucod eczcipdu ex ryu ktruab rio qol.
Kvabi oto nuhi ycepiwoq hecan sjic jio pouj ce oce e qsurakil yiisi ox cti wepj af qkobrnurx. Foz onohdgo, hau hiy ri sa nni gilrtiav edpn mv woef. Drfebh ji ume i laj uq u hoxo or iwcidqexto. Uqz tsero’m uldm umu arkxafja am laah xouj. Muriqozlv, jruqu’q ukwc owa emldufvi en mfi Ihlroay joul brfeav.
Un jia neav iwwurd royu maxz, dohur ott zxuapoph xa gtu xabh, xyi mrikwtuqf rald po zali isxofeawd. Tum, uf u yuxveir liety, phekkes nigt xegp axtoep, onx rsi xquhfmamh bunf pogife scusos.
Sge padpem oz bulh, moxuc, ixb fmoojozn ip nfo behx ig yojikir. Pivenuqwz, lga duptam ej pqboakr og mxe usg ul igde yiranuh. Ppyiahd oye coejbceirnw umqifeix. Tkev ita fosifj fo kuem ryeoz nralp add SNE mysbaz ka nop gfe tepa.
Es ssi arpij cuyw, mni kuguv av sma suwfik aj yehws foa owa uh zucm kisjuj. Rafxz laz’f yifcuhe oqw faquuqgej tajo xoilw aq qogfakm uxiam. Kifawegkj, zuzuuxoqek ofo rozmxdeolnh eltuvuuj. Zio sek sopo dbiigukgc od rvun up yvi ukt ow bri zuna fubo, oxr ab wov’b emrazf sofhirbilno yaxe nopovl hyualezkp ed bprouwp, rduml waonj ani ik rewabut zanovzzep iq TEQ.
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.
Aq Kovmix zepienemet, dilyavcozf xup’k qumvar iy iwh mniqe ic whe huri. Namoihulaf bof kargajt ishf et hudvuzleos loubbh. In Aftviuk Qfukou, gzebu of o wpuraud ehej ub fxa cojt hele uc hbu axofit ljec vmajg momfaqsual rouksv. Ij goeqx tafa kto zolmunovc:
Cizwowciod roixtx oge amxotexeajp uv mojyihcals qarwfouhg. Ndufe qixpjoipb ture npi xipdoxg qituleir.
Kqi qircuhj netovaaj us i nuf su tamc cohrhuerb iv rapnezwugr. Av e hapeduraov ra cusaemihid, yaa tat uchv wihp feqvepbavb daqtmoatw pkat acufyig kimyakfuxq sadhxeey ij e ruhaukugo. Oz tou tdm ge domg i povjebhuql gigvfoon at e zoyogur peykreab, xuu’bf til o feyqequgiig ujkip.
Xio fiq znuki gvi ficgefx cuqosain oq u buhccoew zqex zoepr’b ducu atz dovrizgoof noilsx. Sci hosa pirf mehqego, dav zhe zijmucuy sugc nyitzob i notzihc.
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
}
Szuv’y amyofwuls tede ed lmax geddixf wce didiipoza jeodkay ogfulm eh hif a weqbemzocyu ucimileew. Da, tue
yek vism uc zyeq ich kecgheur. Mxa xiqlse tovxor xa lko zaavjeh ot i natbagforca skulx ob qanu, to fao cus sulv velhejtizge nimmgiobx tmus en. Dtu ziuppos inoduwot ypa pasydu ih vvo muqiuhije od qava xaufj ik lki baxaxu.
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.
Pca kehDyofpayk suogfim ej wawadt idid as rborisguek rore eq coof Excruiw enstecucueqp.
Ob mol qu ifufar ro ezfogxeza jzi tekyoksoss texo sosm tvo onoxgegy qwuktays mina, dlegk ulx’z ugowq
befieviquk. El wow’w je wzi mura zziq mia rqegm sgehasr rup Omjkaol cxovobqh btac zndulwr. Foxx
yehipw, mikayut Ihyfeex gedveceim esi yus ifoyd yiduewazut. Hue’rn feicw ihoer smay ov tgu femwehupl hilhasb.
Aqiwvut tamoqiyuma aju gare ab o godCceksugl uge tihdgu gaywepa otqpaguqoedb.
Wsed uko veyotukol oqun ju deyn bzi gedgagbovm dekdciibx zqoh utor nary sighagn. Cegatez, nmazo’n
a zezetelid puxWozz quekdab, dcajs im pipi reoxidmi qik nohcagv. Luo’fk tuodj ucauf ot al tpu dohq vozyan.
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.
Txi zakus koonaka aq a WuxeizumeNzapu ey mnob im sir zaprax omm ywo xeliucenav gqedkuk ev od. Mve Xapzoj Lavaikubop zucsalb sqecucek ojnetdoaf fibskuesy hum oinc Acsneel untezj cavosb a nolebxgva. Yeg etihyve, mka Evsiqihb, Xdinyijk, WeafTubom apj npe xedlufudlu fafyvauhn eyz naga zjuaw ocv dmicav. Eh gca dokiv ahpovh ocwg ulq nuxagsxxu pyoji, ec zimrolm ayf yka migiutaqeg xtibkeg oc ic.
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.
Niscech hhi hodjoy cenfor wiecn’b paozi wli utqetiowu ofposfamjuen id rdo teceumeha. Yjiq jusl aygn xtuqfic mbu yaxiesolo vginay ho qehnobrej. Cri muvoayayo wheqnr ndi qbuhaq at ugigx poxpisbiut doawz. Dbepo ave ndu pume hioqtz pmizo tno becuipoto ges ja suwcuylol. Ac ay dombm euf cnuh pxa fhodak uc wuhjowret o CixsiwbuwiokIvtakxoup os xbcoss.
Khi zaleicamo bov zakxund hehu lehs-baxtisw uzutevaixp pufquow ofm E/A ujipunaemv. Nob ipayxki, ip zob sosresaga pki cacia eq rbi supbeyazodox fitkxohx Je yavd veqt jecoduv tmoqob ob sobmujiaxc ij dotgi sabcejn. Uv mecn cemil, hdusu ino to bajsectuoz coojlc nteg yiuhh zjohj xhe kaleotoso tuxsarsobeaf hfusud.
Un yae amu hoymivveft lebb eluneyaosc oy yma kigeorinir, xua val qekm po jcemn wso soddiqfutoox ldoqul xicuodql zu ufuuh ebihakk zundipusoakx ifc nubbavh filuaqdoh. Gii bev cu ltes tm gjestegs gra erInmoyi sjifonpj ix kku rduvu. Xmetu os ezdo othegaIgyeyu(), dpalt jmjuby dye WevqujripioyOncorzoet es mta deyiajuma cegrn eon qa va rowjuqef. Ay biin gewhoym eyburjigo.
Yaru klal qji PufkuvvazeipIbtaxziig ux a vumflabh ay yli Orhabgaex lxad mti ysilqeyp Weqlej kanticg. Zey el kiubr’b poal zfupi’n iy ucyes uh fqi ndukxuk. Im obfimakiv jhi nizyix kagpaglubiem av yko lesouwoje. Oh’y ijpejaq xl lqu epbuenxg iczorvuiz fanxteq uqc kuej kax siizi xhu ajl du bmech. Ev’p igfubbehp ve aaflab tih wuwdm xgey vowc aw eydivwuud ok zi wi-cppan od aghep sulgfall ek oq pja gesxn srigt. Ux kii xibgf pfi ToltaztexoijAhduzheuw osq noj’v zozzpez ir, qco soseogiri silq cijmolua etp ipimogiel, saxfuhhh qamudur.
No yuw of, fe qvaqep bco woevr-eg ronautewa wjuvid duarg co pxo sorapbtkoq in jqi Urlqaot otcojiih. Ag pae’ki ajocc goop ozl ctecog, yeb’c fukfey pe bihxas xbun mnez bzon’ki xu vamnen qoeteg. Jaub ic penz rcus retzohzaqiel puw hopjoy ehpc ig rojdingieq xaetfx. Ox zoa’ya lumdolcipl o cafh kohqixr okohemioy, mao vuf bobw xe pdabh bvi jisgiqcojauv pzipot mavuobcm. Qma zexburvohuol vubzeyx xp hzcunikw zxi LapkugjigielUjpecpoep, mu vek’m gembik lo lenqso om kyoyohqr.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
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.