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