We have a Publisher which can publish new values v quickly but processing those values can be slow. This causes values to back up which we don't want. Having an array of multiple values v allows us to combine those into a single value. We cannot miss any published values.
We thus would like to collect values while sink is busy with slowProcessing and do nothing otherwise (i.e. go immediately to slowProcessing if the queue is currently free).
Due to the requirement of running slowProcessing immediately we cannot buffer values using buffer(size:prefetch:whenFull:) or collect(_:options:). As we cannot miss any values we cannot use debounce(for:scheduler:options:) or throttle(for:scheduler:latest:)
Is there a way to achieve this using Swift Combine?
private let snapshotQueue = DispatchQueue(
label: "snapshotQueue",
)
QuickUpdating.$currentSnapshot
.receive(on: snapshotQueue)
.sink { [weak self] currentSnapshot in
slowProcessing(currentSnapshot)
updateUI()
}
.store(in: &cancellables)