When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource

  Рет қаралды 46,238

Lets Build That App

Lets Build That App

5 жыл бұрын

Shared resources are often used across many different tasks in a multithreaded iOS application. If you want to make sure that a share resource is not accessed at the same time, you can guarantee the behavior through a Semaphore. However if your tasks are independent of one another, then using a Dispatch Group is probably the way to go.
AppStore JSON APIs
www.letsbuildthatapp.com/cour...
Tinder Firestore Swipe and Match
www.letsbuildthatapp.com/cour...
Twitter Slide Out Menu Course
www.letsbuildthatapp.com/cour...
Podcasts Course
www.letsbuildthatapp.com/cour...
Intermediate Training Core Data
www.letsbuildthatapp.com/cour...
Instagram Firebase Course
www.letsbuildthatapp.com/cour...
Facebook Group
/ 1240636442694543
Completed Source Code
www.letsbuildthatapp.com/cour...
Instagram: / buildthatapp
Twitter: / buildthatapp
Hackintosh Build
i9 9900k: amzn.to/2CvQj6v
ASUS Z390 Motherboard: amzn.to/2JpaQ2B
16GB Corsair Vengeance RGB RAM: amzn.to/2YbygvJ
Corsair H100i Platinum Water Cooler: amzn.to/2FnbM22
Thermaltake RGB Power Supply: amzn.to/2TgKDTu
Intel 1TB SSD: amzn.to/2Tkdk1V
Corsair 280x Computer Case: amzn.to/2WcjvH2
AMD Radeon 4GB RAM GFX Card: amzn.to/2TYMyAp
LG 34" Widescreen 3440 x 1440: amzn.to/2Tk7q0S
Rode NT1 Microphone: amzn.to/2TZgX1r
Focusrite Audio Interface: amzn.to/2U12gey
Panasonic GH4 DSLR: amzn.to/2U4BrG6
Magewell HDMI to USB3 Capture Card: amzn.to/2Jp2esI

Пікірлер: 61
@TheJhuerta88
@TheJhuerta88 5 жыл бұрын
Great video. I like that you're posting more advanced concepts in your channel.
@luizfad
@luizfad 4 жыл бұрын
Hi Brian, I can't count the number of times I clarified some concept by watching your videos. Thank you so much for all the work you're doing!
@mike_thecatlover
@mike_thecatlover 5 жыл бұрын
Thanks Brian. Please continue videos like this for advanced stuff. Now too many resources only for beginners.
@masonictemple3193
@masonictemple3193 4 жыл бұрын
Thank you so much Brian! This video was incredible, I was able to use it to improve the user experience in our app when loading multiple images into a slideshow!
@GoingWalkabout
@GoingWalkabout 5 жыл бұрын
Extremely useful, especially when I was struggling with 2 async fetches that need to complete before continuing.
@onurisk5617
@onurisk5617 5 жыл бұрын
Advanced topics, interesting problems and great solutions. Thank you Brain
@neklasoctan8681
@neklasoctan8681 5 жыл бұрын
Very detail and clear, Im 3 years iOS developer btw. Thank for give me more detail about this.
@MrBlaq
@MrBlaq 5 жыл бұрын
Props. I appreciate the advanced tutorials.
@Dennisvm82
@Dennisvm82 5 жыл бұрын
Thanks Brian! This was very interesting to watch :-D
@romanromanenko9520
@romanromanenko9520 2 ай бұрын
Hey Brain, thanks for the video! You do good job and help people to learn iOS! I have one comment at 9:23 - it's not confusing, it's because you've set DispatchSemafore value to 0. The Semafore value represents how many treads can be executed at one point of time. When you call semaphore.wait(), you decreased value to -1. If semaphore's value is negative integer it is block execution of task, and system will wait when you release tread/treads which are in an awaiting state. What you did by replacing the lines of code, is at first you increased semaphore value to 1 (which is allow to run tasks on 1 tread at one point of time ), then fetchImage() was executing, then you decreased semaphore value back to 0 by calling semaphore.wait(). So, if you set DispatchSemafore value to 1 and return the order of lines of code, when you call at first semaphore.wait() and after fetchImage() semaphore.signal() - it will work.
@philipplamenov8649
@philipplamenov8649 5 жыл бұрын
You are awesome. Many thanks from Bulgaria :)
@max500k
@max500k 4 жыл бұрын
Thanks Brian! Awesome video
@srikanthburra2512
@srikanthburra2512 5 жыл бұрын
Helpful :) Expecting to share a single video which covers all advanced concepts
@ricardohochman
@ricardohochman 5 жыл бұрын
Awesome video and nice explanation!
@user-gw7xr4kx9g
@user-gw7xr4kx9g 2 жыл бұрын
Amazing video, thank you so much!!!
@ragnacode56
@ragnacode56 5 жыл бұрын
Thank you, Brian! Nice video and good explanation of what is going on. Can you please make more videos about multithreading?
@jensonjohn
@jensonjohn 5 жыл бұрын
Thanks for the information 👍
@pawejanka3526
@pawejanka3526 5 жыл бұрын
This is gold 🎉
@silviuisidor6184
@silviuisidor6184 4 жыл бұрын
Amazing video!!
@mak_whisk
@mak_whisk 3 жыл бұрын
Thank you
@mohanraj2640
@mohanraj2640 4 жыл бұрын
Hi I am a very big fan of your tutorial, In semaphore how to handle error cases, for example, consider there are 3 steps [API Call] if step 1 got error then step 2 and step 3 should not run. How to do this?
@leopapilaya
@leopapilaya 3 жыл бұрын
Nice! Say, in a home page, if I want to display data section from top to bottom orderly, semaphore is the only way, right?
@wassmd84
@wassmd84 4 жыл бұрын
For shared resource example dispatchGroup.wait() can be used
@davidlangley9287
@davidlangley9287 5 жыл бұрын
Very nice
@adityavemuru25
@adityavemuru25 5 жыл бұрын
Hi Brian, I have a doubt. How many API calls can do at a given point of time ?
@ArslanGhaffar
@ArslanGhaffar 4 жыл бұрын
hi whats difference btw dipatch barrier and semophores ?
@ashwinichougale667
@ashwinichougale667 5 жыл бұрын
Hey.. Please provide some tutorial on Apple Carplay Automaker
@hasanirshad3535
@hasanirshad3535 3 жыл бұрын
Hello Brian! Why did u set value 0? What if I set value 1?
@rickisyourbro
@rickisyourbro 5 жыл бұрын
Man this is a good one! xiexie!
@nixonlaine434
@nixonlaine434 5 жыл бұрын
Hey i just wanted to ask a question. I tryed using achor constraints and i did everything exactly like you did on screen but it wont let me use a anchor contraint from the view, which is the main view. i keep getting a error. it says ancesters dont match . EXC_BAD_ACCESS......have you ever had that problem???
@bezeltipis
@bezeltipis 2 жыл бұрын
Is this semaphores technic similar to async await concept?
@comedycentral4333
@comedycentral4333 2 жыл бұрын
I have a question what will happen if we call the above code inside any completion handler in swift instead of viewDidLoad?
@Zainanjum100
@Zainanjum100 5 жыл бұрын
Awesome
@WestCoastAce27
@WestCoastAce27 4 жыл бұрын
Good video - I now know how dispatch groups work - although a little too basic. Could do a little more than just print. Still not sure why they fired in that order. But will use that as a baseline for my application; hopefully it works. I'm trying to do the following: NSURLSESSION to make a req to a db server in the cloud; get the data (XML), parse, then use the data (x,y coordinates) to draw some bar graphs.
@pass1985
@pass1985 3 жыл бұрын
The whole async for semaphores is in one block. Of course it will be executed in order, no semaphore would be needed there. Am I missing something?
@workflowinmind
@workflowinmind 2 жыл бұрын
Bold outfit
@NikeshKannan
@NikeshKannan 3 жыл бұрын
Group.Enter = semaphore.signal which increments semaphore..
@gerardsk8ordie
@gerardsk8ordie 3 жыл бұрын
Is it correct to use a semaphore in the method "cellForItemAt" of a uicollectionview, in order to wait for the previous images to load before new ones can be loaded? I'm loading those images from a shared resource inside the code, as you said in the video.
@gerardsk8ordie
@gerardsk8ordie 3 жыл бұрын
I'll respond to myself. yes.
@hakankumdakc9233
@hakankumdakc9233 2 жыл бұрын
I guess its wrong if you do not change the shared resources. Although I don't know reading at the same time brokes program. Why don't you use operatingqueue?
@user-rd9hd6sw5z
@user-rd9hd6sw5z 5 жыл бұрын
спасибо братишка за видосы)
@looly7555
@looly7555 5 жыл бұрын
Why did you need to but it in a queue ?
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
Semaphores don't make a whole lot of sense when used on the main thread. Using it on a background queue allows for other things to run.
@stevenjemm
@stevenjemm 5 жыл бұрын
Hey Brian, Using swift 5 Result type with DispatchGroups, would I need to include a dispatchGroup.leave() in the success case and the error case as well? Or just do it after the conditional statement checking success and errors?
@sachinyadav-wb6nh
@sachinyadav-wb6nh 5 жыл бұрын
Can u share an example ?
@ciantim2996
@ciantim2996 4 жыл бұрын
DispatchGroup doesn't guarantee that your tasks will complete with the same order as when you submit it. If you want ordered you need to use wait(timeout:) method
@MaiTuongVy
@MaiTuongVy 10 ай бұрын
Hi Brian, I have a question about my case, I would like to find out how the differences between Semaphore and Dispatch Group resolve it The problem: I would like to download 100 images 1. Case 1: If I would like to wait until 100 images are completely downloaded, I should use DispatchGroup 2. Case 2: If I would like to download 5 images and show them in UI, then to the next 5 images. I should use Semaphore. Are they correct?
@jasondubon
@jasondubon 9 ай бұрын
why not use async await ?
@MaiTuongVy
@MaiTuongVy 9 ай бұрын
What do you mean by using async await?@@jasondubon
@MaiTuongVy
@MaiTuongVy 9 ай бұрын
Also, I would like to understand in case we want to choose between DispatchGroup and Semaphore. Are they the correct way to use them? (even maybe they are not good approaches)@@jasondubon
@yeilmusic
@yeilmusic 5 жыл бұрын
Better is dispatch queue barrier
@everythingisamazing5765
@everythingisamazing5765 5 жыл бұрын
I was thinking the same... That seems to be the better solution
@vishalsharma7834
@vishalsharma7834 4 жыл бұрын
Only use Semaphore when you need to manage order of execution of background task. Conclusion !!
@PS-dp8yg
@PS-dp8yg 5 жыл бұрын
If you are a Javascript developer, this is equivalent to async/await.
@RAKESHKUMAR-oe1bp
@RAKESHKUMAR-oe1bp 5 жыл бұрын
Hello man , please one video uploads in firebase chat app create user chat send msg display on top user in Android function
@RostyslavKobizsky
@RostyslavKobizsky 2 жыл бұрын
Thanks, but code style and API design is bad
@rajanmaheshwari
@rajanmaheshwari 3 жыл бұрын
Hi Brian. In your case you are always receiving all 3 elements in shared resource in DispatchGroup irrespective of its order. Same thing I tried with 5 calls and not everytime my shared resource count to 5. It is sometimes 4 or 3 as well. No APIs are failing. Is it due to dispatchGroup member modifying the same state of array so fast that the other one also receive the old state while modifying. Here was the group fb query post I posted. facebook.com/groups/swiftiosdev/1761808553978003/. Can you answer or provide some direction or I have to use Semaphore (Last option). For me ordering is not important
@mohanbabu3289
@mohanbabu3289 4 жыл бұрын
please try to explain slowly then it will better for learners
@alexfrank5331
@alexfrank5331 3 жыл бұрын
10:31 that's a horrible concurrency code. At that point, you should just create a serial queue...
@sachinnautiyal4530
@sachinnautiyal4530 Жыл бұрын
On kzfaq.info/get/bejne/bNh6gcJ2qMqViIE.html, if you change the value from 0 to >1, it will work perfectly fine as it is the number of threads you are allowing it to access. Right now it's 0 that's why it is not print anything. Why it started working was, when you executed 'semaphore.signal()' it incremented the count from 0 to 1, after that execution worked smoothly. Great video BTW, Waiting for more videos from you :)
Threads and Grand Central Dispatch in Swift
22:14
Stewart Lynch
Рет қаралды 21 М.
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Рет қаралды 56 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27
HOW DID HE WIN? 😱
00:33
Topper Guild
Рет қаралды 18 МЛН
Alat Seru Penolong untuk Mimpi Indah Bayi!
00:31
Let's GLOW! Indonesian
Рет қаралды 15 МЛН
Retain Cycles: How to Detect with Instruments Profiler!
13:55
Lets Build That App
Рет қаралды 48 М.
Multithreading в swift с нуля:  урок 11 - GCD  Semaphore
19:53
The Swift Developers
Рет қаралды 8 М.
iOS 15 Async Await JSON Fetching AsncImage Main Actors
31:28
Lets Build That App
Рет қаралды 14 М.
Multithreading в swift с нуля:  урок 12 - GCD Dispatch Group + Bonus
37:44
Loops and Hash Maps Job Preparation Interview Question
15:55
Lets Build That App
Рет қаралды 20 М.
Advanced Swift Generics: Best Solution to Eliminate Code Duplication!
17:18
Lets Build That App
Рет қаралды 38 М.
iOS Dev 17: Semaphore vs DispatchGroup | Swift 5, XCode 11
17:07
Emmanuel Okwara
Рет қаралды 3,2 М.
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27