When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about running Swift async tasks in order, waiting for several tasks to finish, or protecting shared data with a semaphore.
Recommended by 4 people across 4 different months, 2020 to 2022.
What it is
An 11-minute iOS tutorial from the Lets Build That App channel on choosing between DispatchSemaphore and DispatchGroup in Swift. It shows how a semaphore guards a shared resource from simultaneous access, while a dispatch group suits independent tasks that you want to wait on together.
What people said
Recommends Phontaine Judd · 2020-11 · Stack Overflow · on “Several tasks inside a DispatchGroup. Will they run in order?”
Just went through this with my app. I have some async tasks that need to be done in order. The best way to accomplish that is through dispatchGroup() and semaphore() . The basic idea is that dispatchGroup fetches data in no particular order, but semaphore goes in a specific order if you need it to. Here is a good video that demonstrates it: [this video] Some sample code would look like this: let dispatchQueue = DispatchQueue.global(qos: .userInitiated) let dispatchGroup = DispatchGroup() let semaphore = DispatchSemaphore(value: 0) override func viewDidLoad() { dispatchQueue.async { // -------------- // Family members // -------------- // Get members and household information first (esp. payday ti
Recommends Christian W · 2021-02 · Stack Overflow · on “Delay App Progression Until Firebase Data Retrieval Is Finished”
Okay so after more research, I found that one way to keep your app from progressing while server data is downloading and sorting is with DispatchGroup . First of all, create a DispatchGroup variable: let dispatchGroup = DispatchGroup() Then you can "enter" the group at the beginning of the call/function and "leave" the group at the end, once everything has finished processing (such as in completion in a Firebase call). If you're utilizing a loop to sort your data, then make sure to enter dispatchGroup dispatchGroup.enter() every time you enter the loop and leave dispatchGroup dispatchGroup.leave() at the end of each loop iteration. Once you're finished entering and leaving dispatchGroup for
Recommends Mr.SwiftOak · 2021-08 · Stack Overflow · on “How to solve data race/ read and write problem with a help of semaphore/lock?”
@Sasha I slightly modified my answear so It would be more clear. Anyway, you do not have to bother with reads, if there is no parallel write. Here is a video link about accessing shared resource with semaphore [this video] Hopefully it might help you :)
Recommends Shawn Frank · 2022-03 · Stack Overflow · on “How can I know the all the asynchronous tasks are completed?”
Maybe you can checkout out Operation Queues and DispatchGroup. Here is a nice example of DispatchGroup - [this video]
Cites Ryan de Kwaadsteniet · 2021-04 · Stack Overflow · on “How to do function callback in swift”
Instead of outright answering I first want to provide you with a brief understanding of a callback function. To put it simply: In most modern programming languages, functions can be taken as arguments/parameters in another function: function doHomework(subject, finishedHomework()) { print('Started my homework!'); } In the simplified case above, doHomework is to start executing before, the callback function (finishedHomework). If we then call it like this: doHomework('IT', () -> { print('Finished my homework!); }); Or finishedHomework() { print('Finished my homework!') } doHomework('IT', finishedHomework); We will see an output saying we have started our homework printed out on the initial li