I have a publisher where I want to handle receiving the first value one way, and receiving the rest another way. Currently I have the following:
let bar = foo
.first()
.sink { value in
print("first value is \(value)")
}
let baz = foo
.dropFirst()
.sink { value in
print("one of the rest is \(value)")
}
foo.send(1)
foo.send(2)
foo.send(3)
foo.send(4)
foo.send(5)
foo.send(6)
This works. But it forces me to have two subscriptions, one for the first value, and another for the rest. Is there a clever way of combining the two into one subscription (without external flag management). Something like:
let bar = foo
.first()
.sink { value in
print("first value is \(value)")
}
.dropFirst()
.sink {
print("the rest is \(value)")
}
I'm aware the above doesn't make sense (and is probably uglier than the original solution) since a publisher is singular flow that doesn't branch (beyond value, completion, error), but that's due to my lack of creativity, I'm hoping someone here has something that I might be overlooking.