Swift Combine: drop(untilOutputFrom: )

KD Knowledge Diet
2 min readDec 31, 2022

Let’s go ahead and take a look at drop(untilOutputFrom: ) filtering Operator.

So the operator in question is drop(untilOutputFrom: ) and it’s going to drop the events or the value from a sequence until it gets an output from another publisher.

It’s a little bit hard to understand.

drop(untilOutputFrom:)

let taps = PassthroughSubject<Int, Never>()let isReady = PassthroughSubject<Void, Never>()tps.drop(untilOutputFrom: isReady)
.sink {
print($0)
}
(1...10).forEach { n in
taps.send(n) // all values will be ignored, They are dropped
}

I created a PassthroughSubject. When you put dot on the PassthroughSubject, you can find this method. .drop(untilOutputFrom: ). This method takes publisher as an argument. When isReady PassthroughSubject(Publisher) sends an event, then those values won’t be dropped.

Look at the (1...10) loop. the values in the loop are dropped. It never reaches sink() and (1...10) never gets access to those taps.

You can think of isReady publisher as a kind of giving a green flag, indicating that now it’s OK to pass all the values down.

applying the green flag

let taps = PassthroughSubject<Int, Never>()let isReady = PassthroughSubject<Void, Never>()tps.drop(untilOutputFrom: isReady)
.sink {
print($0)
}
(1...10).forEach { n in
if n == 3 {
isReady.send() // 4, 5, 6, 7, 8, 9, 10
}
}

In this example, when isReady sends an event, all the values will be passed to sink(). So, after all, you can think isReady as a signal.

Conclusion

  • You need to pass a publisher to use drop(untilOutputFrom:)
  • The publisher passed as an argument to drop(untilOutputFrom:) is a flag which indicates when it’s ok to pass the values.

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!