Consider the following code (you can c&p it directly into a playground):
class Foo: ObservableObject {
@Published var bar = "bar"
}
let foo = Foo()
let someSubscriber = foo.$bar
.sink { value in
print("value is \(value)")
}
Although Foo has just been initialised once and it's member bar
never changed, the sink executes it's receiveValue
closure immediately. Is there any way to prevent this initial call from happening, e.g. is there an operator I might have overlooked?
You can use the dropFirst
operator. With no argument, it drops just the first output from upstream.
let someSubscriber = foo.$bar
.dropFirst()
.sink { value in
print("value is \(value)")
}
To address issues raised in the comments:
Each implementation of Publisher
can decide what to do with each new subscriber. It is a policy decision, not generally a design deficiency. Different Publisher
s make different decisions. Here are some examples:
PassthroughSubject
doesn't immediately publish anything.CurrentValueSubject
immediately publishes its current value.NSObject.KeyValueObservingPublisher
immediately publishes the current value of the observed property if and only if it is created with the .initial
option.Published.Publisher
(which is the type you get for an @Published
property) publishes the current value of the property immediately.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With