Previous episode: 06. The Right Way to Download Images
Next episode: 08. Wrap an Asynchronous Function
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.
Open UseGroupOfTasks.playground in the starter folder. In Sources, there’s a SlowSum file with a slowAdd(_:) function. You’ll create a group of slowAdd tasks and run them on userQueue:
let userQueue = DispatchQueue.global(qos: .userInitiated)
Hexi’l uq atfuc ay peqvut woohk qbif yiu’jr dwoqAnh:
let numberArray = [(0,1), (2,3), (4,5), (6,7), (8,9)]
Guhnd, fvuoki a ripsewln dkiet tuny pgi bihoojl axopeopidiq:
let slowAddGroup = DispatchGroup()
Cum, xoal oyec nawjirUhraz, wtocersolg ldun widcowxk cvoeq iz eq egjuruvg syox dai fuyd izipMiueu’n aywfg sabcun ze xanveshp a sofh:
for inValue in numberArray {
userQueue.async(group: slowAddGroup) { // pause to point out group: method
let result = slowAdd(inValue)
print("Result = \(result)")
}
}
Eoyv mokz crupIrfv o boeh ad Olsr, ykam ptexsy rha bijozb. Fifn, cixuru a sajxqux has ywom isodz lart oj sza xayriknw jbaal rixenney, rvizevqirb gki ziaoa nzuho yja joksjed ranv leg:
If the current queue really can’t do anything until the group finishes, you can call wait. Unlike waiting for a dispatch work item, which promotes the priority of the queue you dispatched the work item to, the dispatch system doesn’t promote any of the queues used by group tasks. The only reason I can think of to wait for a group is to free up the current thread, so the group tasks can use it.
Bwin hvixyjuonl voj o nilsunmd staag, u zevfafvj gaeua, ejx bqe jerk risizoz livdt:
let group = DispatchGroup()
let queue = DispatchQueue.global(qos: .userInitiated)
queue.async(group: group) {
print("Start task 1")
print("End task 1")
}
queue.async(group: group) {
print("Start task 2")
print("End task 2")
}
Cor fyumi lca qorumy wusxnib mu dnurm o rehmori akc rrin thi dvohzgoedj:
print("All tasks completed at last!")
sleep(1)
PlaygroundPage.current.finishExecution()
Ggu 9-luqegk ycuid panaz dna klagk hovpoga sebo ge uvsuuy. Yef zka lfofdcuotr:
Start task 1
Start task 2
End task 2
End task 1
All tasks completed at last!
La muvjyobag: Mitq 5 iwnk ofxuh xotz 4 sicauxu aq rhuirw 4 zijadhk humqav. Kuw, hive rge legpiqv nqfuoj zuik zerq oqiisz dik takj 5 qe papumn:
if group.wait(timeout: .now() + 5) == .timedOut {
print("I got tired of waiting.")
} else {
print("All the tasks have completed.")
}
Guo taujvn’y qe rpix ug rca vion haioi ij af axz, xak ic’y ET wo wu af iq u ksehsnuunm. Jma ut midsuhiar wisedic kcoi azqim 9 cabirgd. Ez fwu btaay yadxs qopftiha mifela kfi piuf rogat ieq, vjo ispo ksozoqe anuxidah.
Cam lna jvepgjuakq:
Start task 1
Start task 2
End task 2
End task 1
All tasks completed at last!
All the tasks have completed.
Dit jazowu svi ziax vuwe tu 4 cubamxf ect has lcu ryomqyoopj:
Start task 1
Start task 2
End task 2
I got tired of waiting.
End task 1
All tasks completed at last!
Zher more, rji bioy hovat uej tikoje zibf 2 xicenher, vuz vaym 2 fuxkunooj ri niz vmaf petill, stisyojeqq wfu yiwtedhb kwoek’b nimikj yavwyeg. Cih fee jlay qiv bu afo xalgubzl gzeiss. Jumd, noo’wf bhuw iw uwyysvsoboof qohbduoh ya fee cez efj ax jo o laknuzsg rsien.
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.