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

69
Visualizações
Variables dependency using Combine

I have a class with to variables depending on each other. If a variable changes it may change the other and vice versa.

I am using the Combine framework in iOS Swift. So with sink I am listening/subscribe to changes of the publisher.

var store = Set<AnyCancellable>()
class Obs: ObservableObject {
    @Published var pub: Int = 0
}
let ob1 = Obs()
let ob2 = Obs()
    
ob1.$pub
    .dropFirst()  // ignore initial assignment
    .sink { num in
        if ob2.pub != num {
            ob2.pub = num
        }
    }.store(in: &store)
    
ob2.$pub
    .dropFirst()
    .sink { num in
        if ob1.pub != num {
            ob1.pub = num
        }
    }.store(in: &store)
    
ob1.pub = 1

With this code I get a stack overflow. How can I break the "infinite loop"?

Should I use something like semaphores? Or some kind of flag within a tuple? Or are there some special combine filter? Or any other ideas...?

I think I may have some general misunderstanding of the problem...

over 4 years ago · Santiago Trujillo
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