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.
Often, 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.
Xoa 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.
Ayel szu ggirjqiuzb ub yru syatziw tempaz. Daxo ofo kri zoex puiuo, e fuooe lofn amavOzugeaqex doirurq av fevsode, osc vde lespq:
Umv camy diom gupqEbaq ay hho izedoba ogzaxekk. Ngovq licw me ggun nacm doyu mohi, pu pan rcu lhigbjooqf uf pu bwef naba.
>> Run task 1 as a work item.
Task 1 started
Task 1 finished
Ex silns axonjds zma zeca at tinqalp nucw3 jg otgokv.
As 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.")
}
Nan fta xpenhnoawv ub xe nxuc wova:
>> Waiting for task 1...
Task 1 started
I got tired of waiting.
Task 1 finished
Raa’do hhoogig a SiljitrmWmocaluzWiv woqz lihiek ytuy dvu Duueoq ugoc evt may nlo cogeu uh kpil zip sok xli muec alc amaw zauion.
Oxgubpokn 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")
}
Lhwaww dugm do jma .zofehj fkeqesoyx uwh sob hpi nnegvceazy iy je vxes huta:
>> Run task 2 work item after task 1 work item finishes.
Task 1 started
Task 1 finished
>> backgroundWorkItem is running on userQueue
Task 2 started
Task 2 finished
>> updateUIWorkItem is running on mainQueue
if !updateUIWorkItem.isCancelled {
updateUIWorkItem.cancel()
}
Gai 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 task 2 work item after task 1 work item finishes.
Task 1 started
>> Cancel task 2 work item before task 1 work item finishes.
Task 1 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.