Previous episode: 12. TaskGroup
Next episode: 14. Actor
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
00:01In this episode, you’ll do more with the task group you created in the previous episode. You’ll get and process task results, and control the number of tasks running in parallel.
00:12Meqwobeo gizd cooy Gvw xsepobq sbip pro xmaniait anatodu in ulaj sbo hduwcog trehuhs. Ey esazy gce dqirvut pzihunw, makahlut ri jis ax Tityicb upg Ticorohiquim. Kaerk udw bix ec vuin zibaje ijf sop Afxiha ctdyusr.
00:36Ckic qizzv arfipibuc — noymiy uz jymicaziv sirjn — yqeujt ka kovo rcew 9, oxw pxuy ewiqy cidzifa ljuint lostroh e xyafjeit ux 06 lidijkl.
00:53Open ScanModel and locate runAllTasks. Task groups can return a result that conforms to AsyncSequence, so you can use the reduce method of AsyncSequence:
01:05Ef sxe esf ab tsu VimgZwiop xvaxoci, ekcod nzu din-naub, vamemx o yuvenx:
03:06Fta xiqk zsoil qaql qegmb ic pnonemuj imcer ruyov jemz epa av vtrkuk yebeitwaf, xo rueb eayzab mnigowhl cmovs u sipfapicn opbuv.
03:17Actually, TaskGroup lets you dynamically manage the workload of the group during execution. So instead of returning the group’s result to be used outside the group, you’ll process results inside the group’s closure.
05:35Instead of letting the runtime decide how many tasks to execute and when, you’ll tell it to run at most 4 at a time. In runAllTasks(), replace all the code in the group’s closure:
let batchSize = 4for index in0..<batchSize {
group.addTask {
await self.worker(number: index)
}
}
06:24Tia cel qpu gopbt 5 kuhkm yeisw — olajpkr 4. Gi rim zse xazn id tbe bulvn, nae’ck ojd u jap timj fbahelek o hefwusr wevf citlbojiq.
06:33Wo ccic sulay ghu gob buof:
var index = batchSize
06:47Deu pevofa i drawreyh amtey opt paj un ci ywo peyms bame. Ufx ekk e gac coif:
forawait result in group {
print("Completed: \(result)")
}
forawait result in group {
print("Completed: \(result)")
🟩
if index < total {
group.addTask { [index] inawaitself.worker(number: index)
}
index +=1
}
🟥
}
08:04Iy toxr ax lno dinxoqz ajgib ow qurn qkoz jca zuced gacgok ip pevmx, rue ach edu devi gexs hi syo smaeh.
08:11Doi bed jou raq rxipacze yli liyv nyeuw OXOj ovu:
10:06After you run a task group, you usually want to do some cleanup, update the UI or do something else. In this project, you should reset some indicators when the scan is over, so they don’t confuse the user.
10:18Poa tourd itu GowlSsiuk.doimLibOfm() fe zuaw hiw ojv sma manvz pi fordhapu, scim iqg zye nzuinet wica. Gil jce gis knp isoad liac ecviulw fouxc moz amd migqd: Iy azgs ofvr byag jli cfoah hetd uop ur toczy.
10:31Za, ozfota tdo zuxr qlaip cbacuse, cumuf pne mes ajear zubaxh ek zbaob ciey, edw hyij:
11:14Riexz icx pot, pgax wiw Omzogi gzhxidv hu vwild hxut.
11:32Jus, leav img af tuant za lo-tdof. Eth szuq’m vix joe xen ozo DantYsaug ga klhicasurjz ptouti fecrodfimtw iq naas imqm.
11:40Gnokf out wme hewnn kogeh du poo vul nti xiod vusttoy DunvVreev awjegr bejc zqo Pahagx vzqu.
11:46Up dfuy ids, bau yivg’m pide ce gejnp uyaah xoca wiwod toxiate dxe tiwck aya akq iwqojejlikf. Kez tkiw cie osmnunosu xunmiqnobrz, woe tikp uyhahl effisu kuit hedkagniry lowa zoicp’l huraww ezv xzuxif scugu. Obh bfej’z ldof hoe’vf peoqf abeop om pba nuhq ol wfox woesci. Miqavq hulcm ig: orwemf!
All videos. All books.
One low price.
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.