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.
@tonysiu85622 жыл бұрын
wow this comment should be placed on top
@laurapotter63212 жыл бұрын
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)"?
@tonysiu85622 жыл бұрын
@@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
@laurapotter63212 жыл бұрын
@@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?
@tonysiu85622 жыл бұрын
@@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-jr9eo2 жыл бұрын
This channel is a gold mine for those who want to develop and sharpen their iOS development skills! Thanks man!
@iOSAcademy2 жыл бұрын
Thanks!
@jayaseelankumarasamy70932 жыл бұрын
Great one, do more videos related to concurrent and thread concepts
@iOSAcademy2 жыл бұрын
Thanks!
@abdulrahim462 жыл бұрын
@@iOSAcademy Yes, Please do more of these! Thank you.
@muro12345muro2 жыл бұрын
Great content man, keep up the good work & ty for your effort
@iOSAcademy2 жыл бұрын
Thanks
@duongthang51322 жыл бұрын
Awesome, I have been waiting for this
@iOSAcademy2 жыл бұрын
Thanks
@pedroalonsoms2 жыл бұрын
you are the best teacher thanks
@iOSAcademy2 жыл бұрын
Thanks
@laurapotter63212 жыл бұрын
thank you! could you make a video to explain the iOS app lifecycle?!! Thank you again! love this channel so much!
@iOSAcademy2 жыл бұрын
Coming soon
@everyontech27162 жыл бұрын
Great content man
@makwanbarzan70852 жыл бұрын
Well explained. Thanks!!
@iOSAcademy2 жыл бұрын
Youre welcome
@baskaran1802 жыл бұрын
Thanks for posting man!
@iOSAcademy2 жыл бұрын
Youre welcome
@paulancajima2 жыл бұрын
thank you
@iOSAcademy2 жыл бұрын
Youre welcome
@ashokkumar--22062 жыл бұрын
Great video sir....
@iOSAcademy2 жыл бұрын
Thanks
@dasturchi36842 жыл бұрын
Hi Afraz, please put all 2021 swift tutorials in one playlist
@proxa20872 жыл бұрын
I still don’t understand why only “1” printed at the beginning…
@iOSAcademy2 жыл бұрын
Bc it locks the serial queue
@martinq8722 жыл бұрын
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
@iOSAcademy2 жыл бұрын
Haha nice
@jameelshehadeh90112 жыл бұрын
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 Жыл бұрын
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.
@ho6y2 жыл бұрын
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_kous92752 жыл бұрын
More content with Reactive Programming with RxSwift and also about Clean Architecture.
@sharungarg2 жыл бұрын
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 жыл бұрын
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?
@protodimbo2 жыл бұрын
Hey 👋 You forgot to talk about concurrency problems such as race conditions, deadlock etc
@protodimbo2 жыл бұрын
You actually can make whole playlist about concurrency. There is threads, gcd, operations, qos
@iOSAcademy2 жыл бұрын
Follow up video
@sumeetbhujang27562 жыл бұрын
But still it's not clear to me why it printed "1" and not 1,4,5
@iOSAcademy2 жыл бұрын
Bc it locks the serial queue
@stotchmania2 жыл бұрын
@@iOSAcademy Hi. But why does it lock the queue? Thanks!
@csmac3144a2 жыл бұрын
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.
@iOSAcademy2 жыл бұрын
Deadlock
@oleksandrbalan59312 жыл бұрын
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
@iOSAcademy2 жыл бұрын
Thanks for the feedback
@TheScrinn4 ай бұрын
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