Reputation: 602
I have one publisher
public let motionSubject = PassthroughSubject<Bool, Never>()
And i am listening values with
motionManager.motionSubject.sink(receiveValue: { [weak self] isMoving in
self?.isMoving = isMoving
}).store(in: &subscription)
I want to add 2 seconds delay if the published value is "true" because I want to give 2 seconds sound feedback.
I tried to add DispatchQueue.asynafter before motionSubject.send(true) but it didn't work.
Is anyone know how can i achieve this?
Thanks.
Upvotes: 0
Views: 1242
Reputation: 545
What you can use is either .throttle or .debounce, I think in your case throttle will be more beneficial since it does not pause after receiving values. so if you want to do it specifically for the true value you need to do the following:
motionManager.motionSubject
.filter { $0 == true } // will only emit through values which answer this query
.throttle(for: .milliseconds(2000), scheduler: DispatchQueue.main, latest: true) // will publish the latest value after 2 seconds delay since the first pulishment
.sink(receiveValue: { [weak self] isMoving in
self?.isMoving = isMoving
}).store(in: &subscription)
Upvotes: 1