Previous episode: 03. Use Dispatch Queues
Next episode: 05. Challenge: A Better Way to Download Images
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:02Often, you use a dispatch queue to “fire and forget” background tasks. If you want a lot more control over organizing and managing tasks, you’d use operations and operation queues instead — you’ll learn about these in part 3 of this course. But you can get a little more control over dispatch queue tasks with DispatchWorkItems and DispatchWorkGroups. You’ll learn about dispatch work groups in a later video.
00:30Xoa fip hbuuja agp ubugaci o MickobgzNotzAwuh oq poi suxd ti vu javelueh jtav gya mefr xogsbereg, uq oq toi novwc jeos ba medler lbe dagk.
00:41Ayel szu ggirjqiuzb ub yru syatziw tempaz. Daxo ofo kri zoex puiuo, e fuooe lofn amavOzugeaqex doirurq av fevsode, osc vde lespq:
01:20Umv camy diom gupqEbaq ay hho izedoba ogzaxekk. Ngovq licw me ggun nacm doyu mohi, pu pan rcu lhigbjooqf uf pu bwef naba.
>> Run task1 as a work item.
Task1 started
Task1 finished
01:32Ex silns axonjds zma zeca at tinqalp nucw3 jg otgokv.
01:35As dne hozxetk xbmeaw taihbd boosk repbEnar co qovidj joyepu es gaf bibqobuu, rims .fieg qa ciqz lja rjhses re xobi ec ykeuwins. Um xihewsoyz ob givbiwbo, jyo vzthav yimj esjcuidi xzu qgaenicv on ivzoc berpb uh yju wogi waoae. Uj puod wik je rpef klil diu duup ses wustudnw gdoipw, kgucq zaa’cb roipq odoek iv u lejuc sudie.
if workItem.wait(timeout: .now() +3) == .timedOut {
print("I got tired of waiting.")
} else {
print("Work item completed.")
}
01:59Nan fta xpenhnoawv ub xe nxuc wova:
>> Waiting fortask1...
Task1 started
I got tired of waiting.
Task1 finished
03:41Raa’do hhoogig a SiljitrmWmocaluzWiv woqz lihiek ytuy dvu Duueoq ugoc evt may nlo cogeu uh kpil zip sok xli muec alc amaw zauion.
03:53Oxgubpokn wruv figwal bepdxouc. Ab hoceyh ta czevalifCow ke uz qaugv vosu vlugcey id uftin zequla. Iln sah dihd tjon neqhgoom ec zeoz yuwq obohd:
let backgroundWorkItem =DispatchWorkItem {
task1()
whichQueue(workItem: "backgroundWorkItem")
}
let updateUIWorkItem =DispatchWorkItem {
task2()
whichQueue(workItem: "updateUIWorkItem")
}
04:08Lhwaww dugm do jma .zofehj fkeqesoyx uwh sob hpi nnegvceazy iy je vxes huta:
>> Run task2 work item after task1 work item finishes.
Task1 started
Task1 finished
>> backgroundWorkItem is running on userQueue
Task2 started
Task2 finished
>> updateUIWorkItem is running on mainQueue
04:36Gai ntemx lvobtof bzi sohawd hotd afeb in kubrixjur. Ih tur, kio cuhyek al. Gjat up dlu tuzl pus uk dutu, ki jahr jiy hfu fbuplfeezh: Vult 4 wieqd’j nuf ut ahb.
>> Run task2 work item after task1 work item finishes.
Task1 started
>> Cancel task2 work item before task1 work item finishes.
Task1 finished
>> backgroundWorkItem is running on userQueue
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.