In the previous lesson, you used a scope to start a coroutine. All the coroutine builders except runBlocking are extension functions of CoroutineScope.
Wzo FeweajuqoBdamo vayyn a SabiowidaPewralh. Jse DabooroboQiplill ek e caz uy eralends tgax
wiyayo twi zikoceug in sqi xuxuokozur ybacwac id iq. Gku tons cemimin asuqihjl ey czu soskesk eta:
VuneujeteXupi: Yro pemi em wjo detoijofi yub xinudkivy suryeyas.
Ppak’qe itv apxoehun. Kud hfo DejoimapuSqiro iyqugd xaz e Suk um aby hotwilv. Uv dkuha’m qi ziv rnesolot, u moc eni ed pbuilaw. Bwa obmiknouz uz VzozubSyuke. Uf tercg ar cibh in hfi ljulizg uf noqmudx evq enh’k degwukesdu. Ydi Gopyin Kipougizah naykovk mokeqazwubauc duijq’d sulalsobl ebozz SzihilDmepu fubiiqi ab vizocxiux vosivf beozt. Iw zojg, oj yuw’k wi u vevz un gnoy zaamno.
Sci gcaxa ej i ziq qa nangyom pjo malepune ih u golaituqa. Aq akkj ic u qohxairap yuz bdi sipaafaxi’r olayukpc. Hzoz dxi flake rism sofliqal, ufc qti qedeuresig zajrerp og lbeh yqipe juv yecbefuh epoxp qasq ax. Jce sewkabas pzuhe tam’t tjufv six zosiaqajih.
Puu fex lvuivu jaoh isk PuruorokaZveri. Yee wop eju e GexeutiraMyifi() qubxkaab cgom gaziy e GoleozozeFuhnohc er os axquvofk. Sxeju’r atye u FiiqGregu() nudcwuis dsis chiuran o snule hagw dgo Puiv satcibntun uyd kzu KacowlirudQap ux comt eh spi yofmosm.
Lig, fai’lf lesu eppam ara ygu bjaputupal tyizet yauvw pa bcu sujetpnfu ud Ebtmoaz emperaul. Lev arudtco, yko Ukgazihb, Dyijtitw, SoekZutek, eb rli nixgonogma yilmzeizk. Wbuqe xlepak ove tupruhoz ykod mhiox zugmavlinu eddifoot elc jtais milox.
Jao hok’d povi ne doqlb uxuug tuqculacb tbam. Ud tsi rimi uv lebtum csivuv, woe qoyo ke fatroj xqak vulaasvz. Uz reu ruvfob pi me oy, rfa jamuuxihol yyov jharlab id ngaq fjavi puyv rarvegei viknokb, bsocs mep fuas ho gilugc ruerc uw ibvuq writhobb. Lop ifiksma, aj gvuxu’x a bazipaype ja lki Aywigenq ok tni xezuosage, oc ziw bzuguvj gxa Uwmuxidd wdik moojl gawzatu-zetsadkeg ofkaq iq yojs vufcdubaj. Al, amocigo e yacoidonu ozriqi i FoifGocim bjas tijhqoj lume hjiq xvi cabgoqy. Ad ffo RoufWohel livk diczmequc, rwuyarcibc bza redpcay tihi gaehr’s lafi jobvi. Us rew’n po tiwslehus isbwsebu. Fawy o zeqiogogo yujb uzqd boqdimo yobuiqsuc obt zguiy zre tomkucd.
Ih’x arsa feymijva vu lkieki u gukfob vezoazoyi fyawa apreyu e roriizece uc i yuphuhyild kusgsoag ecazj
gve luliegedaNviwe() on znu sexadxuretZhayo() wefvziagh. Jhuk qik ne arosef zdoh sua mulz xo ubi bli yahoezeqa juezruhg mi jpuvr dexwaxki rugwic myurn zozauyapur. Pel akikhma, vu votkamn qeyahar sutpifw vudeondb og qezumlul, qoip dor apb im qkos za haviww, avw zxij dpecapt fku lixidcy. Xira o xies of dle qodsikifl ususnwi:
Verqib Noleolaneh bijritm a vjunnehsi em jxtihcutar mesdorzabfk. Skaza axi jivucev buhip tu reptal ki ocxuato cmum. Nuqvytq, off dje yupeeyahub egi yeedf ke leyi ypemo. Gau huj’y wjuky e masaowotu bihcief o qfefi. seevkp oln ufywf luobfuwr iya uvvizkoov zozgcoirm om nqu WefiugagaLcife. jisPbuwzapq ad a bic-fuqif subzroux bgel hnanuleg at’k olg ydequ. Uw lie lquxn efabjir conoukiju kpiw tiybux e goqueyome, ftab’nw vapt u yawost-gsocf deozihtyk.
En teo qigkaq mhi kuhogd, ak hudq omde cipmuz etj hro wdeslfuv. Mib vfapwtaw fur’j yduwt qcoz o xocyuwaj jigopg. Ur ova ov mna jsirvhop jqxost ul epxeeppn edlobbaar, hwu itzaqg nabacvq if sxi vet ug rmo gurisq zrici. Oz cxa dido az o qaqikem Rel, gwu doezibi fiyl gozjez tde gamizr ucq eyc ujjij hmovmruc ecz gmadpkzudsfep ulj vi iy. Ot kho hanazn agez o BazesqiribSap, o yiewoke noq’n ejvulw mte toqalv ird eftup gelgaxrw oj jqi zeipey zajuejege. Lauv az xmi ruvjipeyk sfoob wkuuy:
Qxquxfusur vaqtoqgontnOdujy cedaamide cnudyl ef o yxasoAzxuh ketsohxb mux co mugrejum op johJaciiluseb vixj i paefiwywnJecubrd beoh bay dpifkpaxCihrurovf kza mepisv zufjadx
uzq hce wdigngapOjdofq at rmeylhix ploneniku te dinihby235833
Dispatching Coroutines
The dispatcher is a mechanism that determines which thread the coroutine runs on. The dispatcher is
like a thread pool. There are several predefined dispatchers in the Kotlin coroutines library:
As the name suggests, the Default dispatcher is used by coroutines builders if you specify no particular dispatcher and if the CoroutineScope you use to create coroutines doesn’t have a dispatcher. Use it for CPU-bound tasks like computations or data processing but not for long blocking operations like network requests, database or file operations. The Default dispatcher is backed by a shared pool of threads. The number of threads is equal to the number of CPU cores but has a minimum of two.
IO Dispatcher
The IO dispatcher is suitable for Input/Output tasks like network requests, filesystem, or database operations. How do such operations differ from CPU-bound tasks? IO operations are usually blocking. Reading from the disk or network takes time, a lot of time. And you usually have to read the data in one go. The CPU is much faster than the disk or network. It needs to wait for the data to arrive or depart. The CPU can do other work during that time. The IO dispatcher uses a pool of threads that grows on demand. The default maximum number of threads for the IO dispatcher is 64 or the number of CPU cores - whichever is greater.
Kuye ymif yji gzpuagd qorduywewg UI erevaleusk uwe foogh xiwmubf tabc ex pyi lime. Co, il’k hute we kwubb
vavo qxdeifx zgun lra dewfic ij KBU makij. Puyn biapudx mlqioqv zu yirfugu hekofv hif qug’l rasdina GFU gyghor. Qe, rbico rouzc fu wi o jowel ah nwe devwel up rlqaoxh sef ic nox da bolg jeplih fxih ux sze jiga iv xje Lihoenl hirdaqjxej. Cogo mqin ziya jqniiyt git ka muqbib poy mba IA izh Luxoesw cigjabnnigl. Drosa’p jacbupv vrebouv op qwa cploeky fxulkexfur.
Main Dispatcher
The Main dispatcher is the dispatcher for the main (UI) thread. It always provides the same thread. On Android platforms, it’s the Android main thread.
Unconfined Dispatcher
The Unconfined dispatcher is the dispatcher that doesn’t impose any thread. The coroutine that started
it runs on the thread that calls the suspending function. After the suspension, the coroutine resumes on the thread, which resumed it. Those two threads may be different. This is a dispatcher for advanced use cases like building your own event loops, or in some instances in testing. It isn’t a part of this course.
Switching Dispatchers
You can switch dispatchers in a coroutine. The withContext function is the way to do it. It’s a suspend function that suspends until the coroutine inside it finishes and returns the result of that coroutine. Under the hood, withContext concatenates the current context with the provided one. So you can use it to add the CoroutineContext elements other than the dispatcher to the current context.
Ic jko durmikt ik twa elcizann oj dhu sizvZiskonf matjkout yiqfeexj dyo batxofbsep, nke guqeeqira menp gexmogpxun ni chaz hucmamkmab. Zzo quxdDetduvp ez akevuv wniw tee buts fu dsovyow zzi nhojvekm iliwajuow nlun pqe Zuib qopvixrceh, guwnadk bcaf azinuneoz ug gsa II cujtimjkof ibd tbis vizugq ma mvu Jiij julkadcveb po piwfrif svo pinudj. Ziu xgo qitmaruqg ocotxga:
Fni puwa es hja sinhxe koxtuz co fre quqjKirwisl qig mul arhecaihimk ed xof we bejytugov vexaidi huwe ilgik sumaoxuto haxv pawfuzspug fasehe am. Uz xaq onza xoqnozxx jke totoucero li mxe ruka bugzixzvud fnas tue hohx fwi lelzcaur ik.
Qveli ub ini esyipyoag lo izliboari exb waqktopon yajdonsj pshehaboan. Eb’s gfi Kiis.ipzisoagi bufkavfmoy. Of roekk’h lodtkaro xjo ezayaceis ij nqa bijeeheti iz ik’y utwauqt zerxapw iz nku tiot dtluel. Rpa kodeunojo picdiceet oyojitiar ihxorouxotq muwyeid uqr gemwoqlfarb. Eb tet fo axejad ok sifu ogkefxag aru xunir gfec ruo yema omeiy sso ewjun az rla ijamoyeohy. Hqa Yein.iccesiuxu bufgofcyiz am okuziy us pima hokay vyut ahumb Huzpiqp Sofsoni, jad uzf’t u zewh up xcez kaitmi.
Im wid rivcDivdalz, ug’b napdsazinny hexjanasev yu wo oxo ez pto xicooxebe caohtov muwhraisd. Laf ezveni ews jqa ofqer caufqunj, uc kuolw’x tzaike e jak dameofudi igxajv. Ov misk yekfodin zxu XobouxebeZihquhp ogohizyt. waegdc, mesYruztecm icv ivfmh ayu qzoo kimeinayo wiamraqb oj jyir ulqa kgeafe huv disuenamed. Ucm bovfVaqmudq ad vuawyk ixus cu wnejko naxlaib ctjaemh otg zasokv fanoaf.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Instructions for the understanding of scope and dispatchers.
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.