SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

How to Ignore AnyPublisher output in swift combine (iOS)

I have a function (which does some network calls like toggling a flag on the server) which returns AnyPublisher<a,b>

func functionWhichReturnsAnyPublisher() -> AnyPublisher<a,b> {
}

In one of the cases, I do not use the output of it.

Is there any alternative without using the sink on it as I am not using any of the output from the call?

If I do not call sink it is not performing the network operation.

functionWhichReturnsAnyPublisher().sink {_ in } receiveValue: { _ in

}.store(in: &subscriptions)

Upvotes: 0

Views: 760

Answers (1)

Paulw11
Paulw11

Reputation: 115041

You must attach a subscriber to a publisher, otherwise the publisher will not start.

You can use ignoreOutput to ignore the output of a publisher.

functionWhichReturnsAnyPublisher().ignoreOutput().store(in: &subscriptions)

You might want to consider still using sink so that you can handle any errors that result from the publisher.

Upvotes: 3

Related Questions