When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource

preview_player
Показать описание
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

Tinder Firestore Swipe and Match

Twitter Slide Out Menu Course

Podcasts Course

Intermediate Training Core Data

Instagram Firebase Course

Facebook Group

Completed Source Code

Hackintosh Build
Рекомендации по теме
Комментарии
Автор

Great video. I like that you're posting more advanced concepts in your channel.

TheJhuerta
Автор

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.

romanromanenko
Автор

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!

luizfad
Автор

Thanks Brian. Please continue videos like this for advanced stuff. Now too many resources only for beginners.

gushiciaihaozhe
Автор

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!

masonictemple
Автор

Very detail and clear, Im 3 years iOS developer btw. Thank for give me more detail about this.

neklasoctan
Автор

Advanced topics, interesting problems and great solutions. Thank you Brain

onurisk
Автор

Extremely useful, especially when I was struggling with 2 async fetches that need to complete before continuing.

GoingWalkabout
Автор

Helpful :) Expecting to share a single video which covers all advanced concepts

srikanthburra
Автор

Props. I appreciate the advanced tutorials.

MrBlaq
Автор

Thank you, Brian! Nice video and good explanation of what is going on. Can you please make more videos about multithreading?

ragnacode
Автор

Nice! Say, in a home page, if I want to display data section from top to bottom orderly, semaphore is the only way, right?

leopapilaya
Автор

You are awesome. Many thanks from Bulgaria :)

philipplamenov
Автор

10:31 that's a horrible concurrency code. At that point, you should just create a serial queue...

alexfrank
Автор

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?

mohanraj
Автор

Hello Brian!
Why did u set value 0?
What if I set value 1?

hasanirshad
Автор

For shared resource example dispatchGroup.wait() can be used

wassmd
Автор

Is this semaphores technic similar to async await concept?

bezeltipis
Автор

I have a question what will happen if we call the above code inside any completion handler in swift instead of viewDidLoad?

comedycentral
Автор

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?

pass