Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda