Questions tagged [dispatchsemaphore]
22 questions
12
votes
1 answer
Safe to signal semaphore before deinitialization just in case?
class SomeViewController: UIViewController {
let semaphore = DispatchSemaphore(value: 1)
deinit {
semaphore.signal() // just in case?
}
func someLongAsyncTask() {
semaphore.wait()
...
…

lurning too koad
- 2,698
- 1
- 17
- 47
8
votes
2 answers
Can I use a DispatchSemaphore to control a thread on main queue?
Apparently I can only use DispatchSemaphore if I deal with different queues. But what if I want to run async code on the same queue (in this case the main queue).
let s = DispatchSemaphore(value : 0)
DispatchQueue.main.async {
…

aneuryzm
- 63,052
- 100
- 273
- 488
3
votes
2 answers
Semaphore in iOS Swift
I am facing an issue in using semaphores on iOS.
I am implementing a feature to execute a series of async methods sequentially, one after another in order.
let semaphore = DispatchSemaphore(value: 1)
semaphore.wait()
performFirstTask {
…

MobX
- 2,640
- 7
- 33
- 54
3
votes
1 answer
Should semaphore wait and signal always be called from separate queues?
I was going through correct implementational details for semaphore using GCD, when one statement from (https://khanlou.com/2016/04/the-GCD-handbook/) confused me: "Calling .wait() will block the thread until .signal() is called. This means that…

coderex
- 61
- 1
- 8
1
vote
0 answers
Asynchronous DispatchSemaphore.wait(timeout:) Task handle alternative
I'm currently looking for a Task based alternative for my code using DispatchSemaphore:
func checkForEvents() -> Bool
let eventSemaphore = DispatchSemaphore(value: 0)
eventService.onEvent = { _ in
eventSemaphore.signal()
}
…

jacksoor
- 353
- 1
- 12
1
vote
1 answer
still can't figure out how to order async tasks for user deletion
So I'm trying to make sure a set of async tasks get executed in a specific order when a user is being deleted.
So what I want to happen is :
Check if user has added guests with their purchase
if user has no guests or any purchases at all, return…

dante
- 105
- 11
1
vote
1 answer
How to make a common resource thread safe when using dispatch group?
I have a class User which needs to be updated every time a user opens the app
class User : NSObject, NSCoding {
var vehicles : [Vehicles]
var bankaccounts : [BankAccounts]
var friends : [Friends]
}
In my home screen ViewController, I…

Pawan
- 90
- 2
- 6
1
vote
1 answer
Can a Dispatch Semaphore inadvertently deadlock itself?
Say we have a shared resource that a bunch of different global queues have access to and, for the sake of this question, we use a Dispatch Semaphore to manage that access. When one of these global queues tells the semaphore to wait, the semaphore…

lurning too koad
- 2,698
- 1
- 17
- 47
1
vote
1 answer
Why does DispatchSemaphore.wait() block this completion handler?
So I've been playing about with NetworkExtension to to make a toy VPN implementation and I ran into an issue with the completion handlers/asynchronously running code. I'll run you through my train of thought/expirments and would appreciate any…

swift_user_167
- 13
- 1
- 4
1
vote
2 answers
How can I use DispatchSemaphore with a Closure
I have a value that looks like this
lazy var authHeaders: [String: String] = {
if shouldTokenBeRefreshed() {
let semaphore = DispatchSemaphore(value: 0)
refreshTokens {
semaphore.signal()
…

Tim J
- 1,211
- 1
- 14
- 31
1
vote
0 answers
Nesting DispatchSemaphore
I'm having trouble with the below pattern. I need to synchronously wait for an initial async request, where the completion block in turn calls a list of async calls where each of the async calls need to wait for the previous one to end before it can…

Jonny
- 15,955
- 18
- 111
- 232
0
votes
1 answer
Current count value of DispatchSemaphore
DispatchSemaphore used to restrict the concurrent access on shared resources. It pauses the thread using counter value. Is it possible to get that counter value?

iroh
- 23
- 5
0
votes
1 answer
How to change a value outside, when inside a Dispatch Semaphore (Edited)
i have a question, and I will tell you as clear as possible:
I need to get an object to my func, create a variable version of it, change some property values one by one, and use new version of it for saving to the cloud. My problem is, when I…

incelemeTRe
- 43
- 1
- 4
0
votes
1 answer
Block HTTP Requests or make them run synchron
I have two HTTP-Requests and i have to execute them in the ordert that I execute the first one, wait till its done and then execute the second one. I used to do this with a loop and a variable inside the call and only if the variable inside the call…

Pjaks
- 251
- 1
- 11
0
votes
0 answers
How to properly deallocate semaphore?
Steps to reproduce crash:
If semaphore.wait() is called and then deinitialize the view controller that owns it before calling semaphore.signal, app crashes.
Getting below error on dinit of the class:
EXC_BAD_INSTRUCTION with the message "BUG IN…

Dhruvil Patel
- 31
- 5