Concurrency using Actors | Swift 5.5 | Async/Await | Data Race

  Переглядів 18,811

iCode

iCode

2 роки тому

Concurrency has always been a pain point for developers and Swift 5.5 has introduced some really cool APIs to make it easy.
In this video, I’ve discussed Actors in detail. For explaining its usage, I took an example having Data Race (seat booking), fixed it using conventional ways (Dispatch Barrier), and rewrote the logic using Actor (for showing the difference that how easy it gets with Actor).
Along the way, I’ve also discussed (and used) Async/Await and Main Actors (for updating the UI from background thread).
Please have a look.

КОМЕНТАРІ: 47
@mazedar_tv
@mazedar_tv 4 місяці тому
Why writing Task in DispatchQueue ? Also 5:31 why we want to halt the main. thread via using `sync` instead of `async` , is it just to return value?
@iampuco445
@iampuco445 Рік тому
You have great didactic abilities … bravo from 🇨🇭
@kishangupta863
@kishangupta863 2 роки тому
Very well explained. Keep up the good work.
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Thanks for the kind words 🙂
@malleshaholeyache4464
@malleshaholeyache4464 Рік тому
Awesome awesome explanation brother 🤩🤩. Thank you so much for your all videos 😍
@benjaminludwig9457
@benjaminludwig9457 9 місяців тому
Hey, thanks so much! Why are the queues at the end still needed? Or if they aren't needed, why are you still using them?
@raymondcordero9718
@raymondcordero9718 Місяць тому
Awesome video. Thanks for sharing your knowledge. Helps a lot
@govind20246
@govind20246 2 роки тому
Actor concept is Nice , it's really interesting, compare to existing techniques.
@puneetpal1466
@puneetpal1466 2 роки тому
Thank you for nice content.
@rooo2000
@rooo2000 2 роки тому
well done very nice explanation 👍
@user-fd4gd8rf3z
@user-fd4gd8rf3z 4 місяці тому
Thank you for awesome video !!!
@vm5lbj23
@vm5lbj23 2 роки тому
Learnt something new today. Thank you. What I am more curious about is how the actor is working internally?
@apoorvamishra2047
@apoorvamishra2047 2 роки тому
Thanks for the nice content
@pawanmanjani1298
@pawanmanjani1298 2 роки тому
Superb keep posting💪
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Sure 🙂
@user-bb1ke2sw1j
@user-bb1ke2sw1j 9 місяців тому
superbb
@yogeshrathore919
@yogeshrathore919 2 роки тому
Very nice explanation.
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Thanks Yogesh 🙂
@rahuljamba5846
@rahuljamba5846 3 місяці тому
Thanks Palav
@RAHULSHARMA-iu1xq
@RAHULSHARMA-iu1xq 2 роки тому
Nice explanation 👍
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Thanks 🙂
@dhav7634
@dhav7634 Рік тому
Great tutorial. I observed that even when I did not create add keyword "MainActor" and just called the updateLabel method from my Task block, I did NOT get a crash and the setting up the break point inside updateLabel showed that it was on main thread. How is that possible?
@pramanshugoel6606
@pramanshugoel6606 2 роки тому
Hey pallev, Thanks for the video. Have one question why you used sync on both operations with barrier.I think same could be achieved without using barrier on sync operation (as they already in sync)or async operation with barrier?
@tusharlama6085
@tusharlama6085 2 роки тому
Use sync function for read to block current thread, while using async function with barrier flag for write to return to current queue. This is good for when multiple reads is preferred when there is no write.
@shiwivishu4434
@shiwivishu4434 2 роки тому
Hey pallev, Thanks for this great video. now I have a clear concept of Actor. I would really appreciate if you can come up with one video of Closures. thanks
@madhavkure3361
@madhavkure3361 2 роки тому
Very nice sir. thanks
@akshaykumar-qj2kx
@akshaykumar-qj2kx 2 роки тому
Great Job 👏
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Thanks Akshay 🙂
@aidagarcia8823
@aidagarcia8823 4 місяці тому
Man this guy is good
@sheldon94666
@sheldon94666 Рік тому
Interesting trade off. The barrier was neat in the old Flight class. Now the burden to make it properly called, the caller are forced to call with async await.
@abhishekboosarapu6071
@abhishekboosarapu6071 2 роки тому
Hi Pallav, Great fan of yours. In a couple of interviews, I faced this question. "How do you convert struct to a class? or atleast give some functionalities of class to struct." I searched a lot through web but couldn't find any answer. Could you please help me with this.
@w0mblemania
@w0mblemania Рік тому
TBH, it's not a very good question from the interviewer. I guess they could be trying to make you think, but still, not a useful question. The only good response I would give is "what, exactly, do you want your class to do"? And then explain the differences between structs and classes, and why you don't actually want to be "converting" structs to classes.
@Purnao6151
@Purnao6151 2 роки тому
I learnt the latest topic. Thanks for sharing.
@raysubject
@raysubject 10 місяців тому
And chance this is usable also for communication between realtime audio thread and main thread ? I mean if you are trying to communicated between realtime audio thread and main thread there are some forbidden things like using of MUTEX - so any chance all this Actors aren’t using MUTEX ?
@saurabhsierra9195
@saurabhsierra9195 2 роки тому
Impressive ....once again.👏👏👏
@aparnarvd4343
@aparnarvd4343 2 роки тому
Thank you for sharing the valuable information
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Glad that you found it useful 🙂
@marlonvonansale6457
@marlonvonansale6457 2 роки тому
thanks for the video keep up the good work :)
@manjeetchoudhary5860
@manjeetchoudhary5860 3 місяці тому
🎉🎉🎉🎉❤
@ilyablinov4979
@ilyablinov4979 2 роки тому
Thanks for this lesson)))
@vineetsingh8201
@vineetsingh8201 2 роки тому
👍
@sandeepsuthar4582
@sandeepsuthar4582 2 роки тому
When should we use weak and unowned in swift?
@iCode_Happy_Coding
@iCode_Happy_Coding 2 роки тому
Mostly, when you have guarantee that the object won't be nil, unowned can be used. Otherwise, prefer weak to be safe. There are other details too.. I'll try covering them in the video that I'll do weak, unowned etc. Thanks for the suggestion 🙂
@sandeepsuthar4582
@sandeepsuthar4582 2 роки тому
@@iCode_Happy_Coding I think if i will follow your videos, will become master certainly. Please guide us how to prepare DSA for beginner to expert level and then solve interview’s online test with all test cases. I have seen some UKposts lectures but i need something that covert step by step and all content should have at single place. I am rejected 10 times from Nagarro and other reputed company’s online test. Thank you
@harishlakhwani5072
@harishlakhwani5072 Рік тому
Hi Pallav, Thanks for the video. I tried with same approach but surprisingly it worked if I used the 2 queue like in your video but whenever I tried with 3 queue it is not working. I am sharing the code snippet for the same. Share your thought on it. actor Flight { var availableSeats = ["1A", "2B", "3C"] func bookSeat() -> String { let bookedSeat = availableSeats.first ?? "" availableSeats.removeFirst() return bookedSeat } func getAvailableSeats() -> [String] { availableSeats } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let flight = Flight() let queue1 = DispatchQueue(label: "queue1") let queue2 = DispatchQueue(label: "queue2") let queue3 = DispatchQueue(label: "queue3") queue1.async { Task { let availableSeats = await flight.getAvailableSeats() print("Available seats \(availableSeats)") } } queue2.async { Task { let bookedSeat = await flight.bookSeat() print("Booked seat \(bookedSeat)") } } queue3.async { Task { let availableSeats = await flight.getAvailableSeats() print("Available seats after booking \(availableSeats)") } } } }
@joshuawalker7530
@joshuawalker7530 Рік тому
Why do you think this didn't work? It works for me. I get Booked seat 1A Available seats after booking ["2B", "3C"] Available seats ["2B", "3C"] In the console, which is what I would expect. Only one task is booking, the others are waiting, then printing the available seats.
@arunj3344
@arunj3344 Рік тому
@@joshuawalker7530 Can you run 5 times and check you are getting the same results, Its not working for me as well with actor giving different results
Your Brain 🧠 on Swift Concurrency - iOS Conf SG 2023
30:38
iOS Conf SG
Переглядів 7 тис.
Эффект Карбонаро и пончики
01:01
История одного вокалиста
Переглядів 8 млн
How to use AsyncStream in Swift | Swift Concurrency #18
26:32
Swiftful Thinking
Переглядів 4,1 тис.
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
Design Patterns | Factory Design Pattern | What, Why and How
14:53
How do Actors work in Swift?
19:05
Swift and Tips
Переглядів 3,7 тис.
Intro to Sendable in Swift 5 (2022)
11:23
iOS Academy
Переглядів 12 тис.
Илья Чикмарев - async/await в Swift
29:19
CocoaHeads
Переглядів 10 тис.