DispatchQueues: Serial, Concurrent, Async, & Sync - Overview

  Рет қаралды 25,568

iOS Academy

iOS Academy

Күн бұрын

Пікірлер: 53
@brunoandrade-ca
@brunoandrade-ca 2 жыл бұрын
You did not explain sync vs async really well. The main difference between those is: Sync: Blocks the caller queue until the task is done (serial or concurrent. Async: Does not block the caller queue. Serial queue: Executes tasks one after the other in just one thread Concurrent queue: Executes tasks in parallel in a different queue's thread On your first example, the main reason why it printed "1" it's because you can never call sync{} from the same queue AND to the same queue (serial or concurrent). You'll end up with a deadlock and a crash most of the time.
@tonysiu8562
@tonysiu8562 2 жыл бұрын
wow this comment should be placed on top
@laurapotter6321
@laurapotter6321 2 жыл бұрын
could you explain a bit more on the 1st example please? What do you mean "you can never call sync{} from the same queue AND to the same queue (serial or concurrent)"?
@tonysiu8562
@tonysiu8562 2 жыл бұрын
@@laurapotter6321 if u call sync from the same queue and to the same queue. The sync routine will need to start after retuning from the sync routine. And the sync function call cannot return until the sync routine finished. So it becomes a dead lock
@laurapotter6321
@laurapotter6321 2 жыл бұрын
@@tonysiu8562 but in his 1st example, there is only one sync called on the queue right? Where is “from and to the same queue ” coming from?
@tonysiu8562
@tonysiu8562 2 жыл бұрын
@@laurapotter6321 In the example at the very beginning of this video, it is serial queue. Serial means, one task has to be completed before the next task can start. .async{} is retuning even the task isnt started. since the sync{print "2"} is inside async{ print"1" ... } the task in the print"2" routine cannot start before the everything in print"1" routine can finish runnin.
@Karan-jr9eo
@Karan-jr9eo 2 жыл бұрын
This channel is a gold mine for those who want to develop and sharpen their iOS development skills! Thanks man!
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks!
@jayaseelankumarasamy7093
@jayaseelankumarasamy7093 2 жыл бұрын
Great one, do more videos related to concurrent and thread concepts
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks!
@abdulrahim46
@abdulrahim46 2 жыл бұрын
@@iOSAcademy Yes, Please do more of these! Thank you.
@muro12345muro
@muro12345muro 2 жыл бұрын
Great content man, keep up the good work & ty for your effort
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks
@duongthang5132
@duongthang5132 2 жыл бұрын
Awesome, I have been waiting for this
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks
@pedroalonsoms
@pedroalonsoms 2 жыл бұрын
you are the best teacher thanks
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks
@laurapotter6321
@laurapotter6321 2 жыл бұрын
thank you! could you make a video to explain the iOS app lifecycle?!! Thank you again! love this channel so much!
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Coming soon
@everyontech2716
@everyontech2716 2 жыл бұрын
Great content man
@makwanbarzan7085
@makwanbarzan7085 2 жыл бұрын
Well explained. Thanks!!
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Youre welcome
@baskaran180
@baskaran180 2 жыл бұрын
Thanks for posting man!
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Youre welcome
@paulancajima
@paulancajima 2 жыл бұрын
thank you
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Youre welcome
@ashokkumar--2206
@ashokkumar--2206 2 жыл бұрын
Great video sir....
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks
@dasturchi3684
@dasturchi3684 2 жыл бұрын
Hi Afraz, please put all 2021 swift tutorials in one playlist
@proxa2087
@proxa2087 2 жыл бұрын
I still don’t understand why only “1” printed at the beginning…
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Bc it locks the serial queue
@martinq872
@martinq872 2 жыл бұрын
Oh, my god... I always hard to understand this. thx so much 😭 actually many answer replied on your community and I am one of them LOL
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Haha nice
@jameelshehadeh9011
@jameelshehadeh9011 2 жыл бұрын
i get it why 1 is printed in the first block of code i.e queue.async { print("1") queue.sync { print("2") queue.sync{ print("3") } } } but why the last two async blocks containing "4" and "5" are not executed despite them being called out of the first scope ? i tried to think about it and thought because the queue will be busy performing print("1") but it supposed to be serial so once its finished the first block it will move to execute the next two blocks i believe there is something i am misunderstanding here its confusing little bit, would appreciate some help
@osmansuliman5226
@osmansuliman5226 Жыл бұрын
Since the first closure submitted to the serial queue contains a sync call, it blocks the serial queue until the closure has finished executing. This means that the async calls for "4" and "5" are not executed until the first closure has finished executing.
@ho6y
@ho6y 2 жыл бұрын
queue.sync refers to that particular queue doesn't it? instead if you specifically point to queue.main.sync would be the same than not using the queue itself since you're poining right to the main thread am i right?
@rev_kous9275
@rev_kous9275 2 жыл бұрын
More content with Reactive Programming with RxSwift and also about Clean Architecture.
@sharungarg
@sharungarg 2 жыл бұрын
So to understand it, when we are calling sync, we are telling the queue to make itself available, free from any ongoing tasks (sync or async) or just complete the current task and make itself available. But as the sync block was called inside the async block, although the queue can put the async operation on hold and take other tasks, but cannot make itself completely available for the sync block to run. The sync block is demanding the full availability of the queue, but the ongoing async block cannot complete unless that sync block completes. Can someone please confirm it?
@حسن-ي2ر2ص
@حسن-ي2ر2ص 2 жыл бұрын
I tried before with a certain api to make a request serially by write a code as you did but it did not work. Does "task.resume()" or "session" have an effect on that?
@protodimbo
@protodimbo 2 жыл бұрын
Hey 👋 You forgot to talk about concurrency problems such as race conditions, deadlock etc
@protodimbo
@protodimbo 2 жыл бұрын
You actually can make whole playlist about concurrency. There is threads, gcd, operations, qos
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Follow up video
@sumeetbhujang2756
@sumeetbhujang2756 2 жыл бұрын
But still it's not clear to me why it printed "1" and not 1,4,5
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Bc it locks the serial queue
@stotchmania
@stotchmania 2 жыл бұрын
@@iOSAcademy Hi. But why does it lock the queue? Thanks!
@csmac3144a
@csmac3144a 2 жыл бұрын
Sorry but you didn't explain many things. Why did the 1 print? The difference between sync and async is not clear from your explanation or example.
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Deadlock
@oleksandrbalan5931
@oleksandrbalan5931 2 жыл бұрын
I respect your intention to try to explain things, but your explanation of sync/async is just bad and might only confuse fresh iOS devs
@iOSAcademy
@iOSAcademy 2 жыл бұрын
Thanks for the feedback
@TheScrinn
@TheScrinn 4 ай бұрын
queue.sync - impossible to call, code crashes. Did tried to copy code from screen as is and that crashes in runtime. don't spend time on video, marked with dislike
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 86 МЛН
From Small To Giant 0%🍫 VS 100%🍫 #katebrush #shorts #gummy
00:19
You hate DispatchQueue.main.async { } ? Good news, @MainActor killed it ☠️
11:51
Threads and Grand Central Dispatch in Swift
22:14
Stewart Lynch
Рет қаралды 22 М.
Main Thread, Sync vs Async and Deadlocks in Swift
9:49
iCode
Рет қаралды 13 М.
iOS Concurrency and Threading - iOS Interview Question - Swift
7:50
Strong vs. Weak Swift 5: What is Weak Self (Xcode 11, 2020)
16:41
Лекция 17 - GCD, Многопоточность, Queue, QoS
1:12:00
В’ячеслав Білий
Рет қаралды 9 М.
Concurrency and Grand Central Dispatch in iOS
12:03
Clarissa Calderon - Swift Bits
Рет қаралды 1,9 М.
When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource
11:11
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 86 МЛН