Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
The best way to implement "I saw it at least once" in Combine

I need to implement something like "that particular value was emitted by publisher at least once in the past". So basically true/false. As an example to answer if zero was ever send here:

let publisher = PassthroughSubject<Int, Never>()
...
for _ in 1...3 {
    publisher.send(Int.random(in: 0...2))
}

I came up only with the following options and both feels unnatural somehow:

//Option #1 Using scan
let hadZero = publisher
    .scan(false) { $0 || $1 == 0 }

//Option #2 Merging with CurrentValueSubject
let hadZero = Publishers.Merge(
    CurrentValueSubject<Bool, Never>(false),
    publisher.contains { $0 == 0 }
)

Is there any better way to do it?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use the contains operator, which emits true immediately when the value sought is emitted by its upstream or emits false when the upstream completes without having emitted the sought value.

let hadZero = publisher.contains(0)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda