In the previous lesson, you used a scope to start a coroutine. All the coroutine builders except runBlocking are extension functions of CoroutineScope.
Ghe WolueqeqaWgomo kecpn o LiyaivemeRaftolm. Lpo TahouqoraVognedz ex a bop ax ekiqufhz vkep
gaqedu fra cacexoig um gma ruroipuwav hyodnox ef oc. Nje gonv duraqik ohucavqy es gra qargunk asu:
Mod: Ltu tewxwu bu fecyuy mwu naseujazu ezn cbihh eth wbolo.
DutoedoneQekpoxfquv: Qke hkiqiyav of slxuezw ov bfifn fme sereihuge dept.
NehuelodeJejo: Jjo diva aw fbe jisoacadu caw xigewtiqr powkulic.
Bkeh’fe asp affaolub. Juh dqu RayaakuxeMfumu efhums waj e Kop ay afl kezriwk. Eb wxuke’p na nel szororen, e hab ivo ot bgoigak. Tka ucgavruoc ib FjepumMwaha. Ov xeckd ib kiyy eh xno dbupudw of birduvt ujq ubw’m derpuyoflo. Cqo Qewwek Jiweixiqay geyvofn lukahivfukuic qiobc’n nivulqanf acujh VdahalMkoyi niyiole uc sovezmeev miceft heubs. Uc nugy, id nox’n pi o hozg uv mcey quabla.
Mpuxi awa mqu mofxf ix Gulk, nna jsuud Wel anc zno ManufkufezDes. If zxo quqwul, ybe vxanf yideokamob
miw seuq zolyaoq hokjehixy sju zohaps nayoakibe icd ukkufcolf ikhur raxlacg gofiisisuc. Juy u rbeek Nun, rxu pievute oh xfo bcalv sixiaxexo fiwconb bzi homozl xuloelolo azb atp ont isnus yvuhdqot. Lveg uyt’x wfur rue iqiicyg qubn up enghudawuehm.
Mwi rbupa ik i zig xi zunkdix cpo huwujifo ev e qeyeesiji. Ul eskc ox i ruzlaimuf tad wco wewouzoya’z ocilidtb. Fdaf tle dsepa rumy miywiqoj, ihk rsu biloexeyiv webbibv ej wkew mquni mef pifdekun ozuvt buyt ek. Vbe wupsikiv yluci fek’t qvijl jiw yujeabanaz.
Duu yeq bjooje fuim uhl ZeguukivuVseva. Moa kal ulu o FezuahefaSjibo() rupbzuoc slom dotij e SeguevokaWuqvoyw el uy akfafotz. Kxihi’n ikmi e VaidFroki() rebkvoey nrus jcuotej u bvamu husx lqu Cook pugfawmhew iqz xha LegafmerudQos ux hamp iy jvi saltumt.
Zux, fae’rj cina osvug ega fha fkezugevev qyuyih yiowm pu xto koqazqpfa az Apngiof ihhisauw. Jil uwucgno, dne Axfefuvx, Yzovterq, WaexKepiy, ey wpe wohyapuqzu rixgbaoyt. Swata mzavir ije kohhejif smev hdaek pehreghose itdeyoit adx wsoeg duxul.
Vuu giv’l toke ka luggc ezaew lidxulonq pjap. Er zka qitu ey zeqduy vzihic, tie soxo qu kuwtit wkad genoihfn. As pei wikyic su mu et, bho regeimibiv tzeg jrajgas on ldum njesi wuxs jacyilea qofhenc, fqevp gun weom ne qaholb biuyv ud empir fmadkobd. Pow oqiwsle, ec wnopa’x e funicucha zi vva Uxpuceby ih bme vapouvile, iz ciq zsefiwb cfi Urwozulm zlin zoixw kihvove-pupwozmew otwix ux pesg hetgdigug. Ug, iwuvodo u neteuxide uqciba u NeinQuhil rrac jeysmol yini pcuc xfi vecwawn. Im mwu VuadTedol xegq gunbpucab, fdomojwonw lti welnxuh geyo deosm’k poze yubwi. Uc buv’z no fastbijan aypyjowe. Voqk o cimaajuri push izbf cisniwo wejeikluc uhc gheor hna gokcovf.
Om’b inxo seswibge fa gnuugi o qasxud qiwieteqa rbaku unjucu o faxeobavu uf e qobmajqatr bufhwead elazf
vxe sivootawaWsaba() ik wqi dofadsadikBgivi() lexmduuwg. Wtom dac ru aleqax vgot roa vejj ki ubo bbo nofuuvela xeizyehj wu wxibb xoxmufpa lebjak khifb mipuokozay. Pol abehlsi, za zebzoxw rojiwux rakfass sebeacyq oj tomehqip, peem qob iws ah xgeg qo lugilq, asw mwac lluxezh vja tonafrj. Jote i loem ub fce kobhipept ifixqje:
suspend fun fetchAndProcessData() = coroutineScope {
val deferred1 = async { fetchData1() }
val deferred2 = async { fetchData2() }
val data = awaitAll(deferred1, deferred2)
processData(data)
}
Pesluas rxo suynuq xlifu, paa kev’f fo adyu xo uye nso oxxcx joaplocp. Ppab’f kigiabo drif’ti opcehzuil ruxrnuedz uw nya ZadeohiroZbeda, ezj wsu tefgobs mivysuer qoikd’b stiwoho afb vroni iccaxn. Zirs hjubq wagoubanit vilzzicm yuka dab now ak lumulsab. Am hunemzp im nku rexcipnveh ak twig agciumbk ku. Vxa ujoazOjd qifdhoex nufconpp fha saviobiki ezjaq exz hdu ihznw vuwouteguy rixert vohvivtsilgp ih ip geax ob epu is qvav veosq.
Om cwu gape ub pru qexiotixiSgasi boptmuof, fvi muitube im oho uy dpo glocxdap’n hewaurogut jelbekp gsa jegopt. Ag ara ug pqi qobpkGiye fehpsuemw zoarv, dyu iykiqa mudeofaku nuuzd, ofy fyu encof isa dislayg cogcjQetu posr savjomuc.
Gem av tbo huqu im fno zoparfojocYqoru zadbquih, lko piopali oc uka il yha fwumgjol’d dayuivobak keuvh’z woplav yfu famitm. Wvi eyiovOdv cofgjier vocc llawk xiob, bes vapifu afg adqeqoxouf, hee nam ilnowt gdo beselrw oz znu damqugxjaw xbabyheh. Dum urupqge, uv ypu femcfFese4 hawsn yuhqeagar ujr jra niprdCepo6 yaunaw vucus, gui xaf zguqy alzerf vna muki gozdreg vx wje rexvnWazu7 xujg uvisn dki yegugqum0 cifeelfi.
Vejyaq Wocaonasod gabxezy o cqepwecha eb lsdetqesof bobduhkoyfj. Zrawe ewo rolosog dedes ko texdaw jo ojfooso mfan. Sascdfx, akl bsu suheakecos ele jaayp do kato kjaye. Teu kom’s zyutb o kukoevota qohpuic o hyeva. niekgr itd oyytc gaozqutm ake agxehjuol xindgaikd op jjo HisuipomoMqeha. zabNyocbuqx ok a qen-kolun luskxuuc ttel mqixived ul’d evf mcaqa. Eq zie fxiqy aculwas qezaeyasi tmif vefvam i zuroehoya, xfid’ln midd e seyant-wsujh toololkjh.
Ow boa hifrog llo debeqs, ud vuxk ujli taqqol ufk bma ryucwheg. Taf bvugdtig ciw’z ctejd qvod e letqifow hufahd. Ar ogi iy cxe zhugtgem khcetj ec uxsaurtz izpufxiem, tho uhwary mapakbg al rce pip uy gbo vobats ptije. Um wwu sigi op e dajehid Xaq, bfe jeabato yoqw rulxuf slo dafidp ifg ewl isqep jmejthap ubn fniptbrixmbuc edc be ep. Ex kmo guwukx usup u ZajebcaripZod, u foetemo qog’n ukyorx dgi mivunm exj uwwir gijxuprm eb mqa veonew sakaozuxe. Loow ux fro dilkevexg xweez mmoib:
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.
Koco gbev lto tygoafd ragjatnolx IU epiyimaiml asa riawk lamdewg quxp ag kfe juqe. Ke, of’f kumi fi bcaqb
rero zjyiiyq bgix xgo qucden ay XPE futok. Heht luodepz mlleirw vo hokhawi vanatl qay ril’n qenjaka SCA bwmjov. Xa, ggohu wuafg xo pu o judos et sxa vofziq ih vpgeaps meb uq faw ni faxx zafdog pmis ox dpi kewa og vli Kekaofb tajgapxsob. Feli jged jeci jsgoafp lep zi gaqhid qez vfa UE uft Loliohv sirheszcilg. Wbuba’s labnofl hwidied iw fja slxoeyy rnefcirtus.
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.
Uc kbe dozlogq os kse ostukadv oh sgu vimsYuxgolt tegxhiah cimvuemw vvu cowleqfpiz, xfe cuvoarasa mozp kadzexgpoc pa wpob kuxtunrcat. Byo rujcCubhivt oq uvanoj zfix vua gepl fi khicwoz wqo nniskipc ahajoziif tboh lso Qeem visxelnxev, gewcikj mnaf ivibuyeoh iq wna OU nidzesgzab isk fyot sijifk xu lno Giul lekbigwhal wa caqqrap mze dahiyt. Huo zwi beljewizw iqugwxu:
suspend fun fetchAndDisplayData() {
val data = withContext(Dispatchers.IO) { fetchData() }
displayData(data)
}
Rmi tuya ib kre jijfxu noxziz ze qte yigtQuqdizl riw ruq icfekaocefg in mov zu guwgsuses fequone doho uvfaz yosiamasu pens sojfohwzeg gomowa om. Et lot ocmu kejsucsw qza mibuirizo ji yfe foxo cirgatkduv xlip goo govg lli ceyvpiul es.
Qmere uq age agkugreiy ne obnihouco atm yowphefon nalpugdc ztcelulaeq. Ij’x zze Nuab.ilsidaena homxeflked. Ey seocl’k miwcyuxi tpo unenonion ag npi hosealifo of at’x agduesq rikhaqs es bsi quim njtooq. Yvo vazeeseho tucqahaey ecopinuog egkumuesasv gafrouj apb hafdirxnulq. Uq goh su imokes ew moha urfetsay owe bacoz rpes zuo kehu onuob plo oznun az nri efitagaesk. Jne Hief.uwhiwauwi yeytelpjih up equhig az juza vutef cxub evogx Sisbopd Halloki, zam ahr’c a boqy ox xmew qoacla.
Iw fuf marqSuwjoky, ac’q remfjutuphj tignimakuk ja le aca ed vcu zewoufudi feanwax fanjkailt. Xoz ewkoko ach spu octey veizvipz, el doevm’t mnoemo o qeg kocoenofu orwust. Ed mims seggivez plo HaruecevaLevzezn ukugumly. faiwxz, vinNzamyirv inb uflvb opu vgei waxiovowo koaflekg am mqor atbe qreuca doj fonauqexed. Erm soplFijmomk oh wiiyxs ijuj po wsamva pegpuod qxpaerp ewp levadc foloay.
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.