Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine convert Just to AnyPublisher

Tags:

ios

swift

combine

How to convert Just<[Int]> to AnyPublisher<[Int], Error>. When I use eraseToAnyPublisher() the type is AnyPublisher<[Int], Never> which is not the same as AnyPublisher<[Int], Error>

For example I have a simple function which I want to mock temporary

func getAllIds() -> AnyPublisher<[Int], Error> {
    return Just<[Int]>([]).eraseToAnyPublisher()
}

Any ideas?

like image 439
mkowal87 Avatar asked Mar 05 '20 17:03

mkowal87


People also ask

What is AnyPublisher in combine?

AnyPublisher is a concrete implementation of Publisher that has no significant properties of its own, and passes through elements and completion values from its upstream publisher. Use AnyPublisher to wrap a publisher whose type has details you don't want to expose across API boundaries, such as different modules.

What is Just publisher?

A publisher that emits an output to each subscriber just once, and then finishes.

What is swift combine?

Overview. The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.


1 Answers

Use .setFailureType. The situation you are in is exactly what it is for:

Just([Int]())
    .setFailureType(to: Error.self)
    .eraseToAnyPublisher()
like image 78
Dmitriy Lupych Avatar answered Nov 22 '22 23:11

Dmitriy Lupych