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

254
Visualizações
RxSwift - What if a Behavior Subject receives 2 values from different threads at the exact same time?

Two of my subjects running network calls on separate threads will update another subject once they fetch their data. My question is, what would happen if they both send a value to the subject at the exact same time?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From the book Intro to Rx:

Avoid the use of the subject types. Rx is effectively a functional programming paradigm. Using subjects means we are now managing state, which is potentially mutating. Dealing with both mutating state and asynchronous programming at the same time is very hard to get right. Furthermore, many of the operators (extension methods) have been carefully written to ensure that correct and consistent lifetime of subscriptions and sequences is maintained; when you introduce subjects, you can break this.

IE, Subjects are not thread safe. If you call on(_:) on a Subject from two different threads, they can cause a synchronization anomaly and break the observable sequence grammar.

In other words, you are risking a race condition. Best case, you just get a warning while in debug mode. Worst case, your app crashes in some mysterious unidentifiable way, far from where the problem actually occurred.

Another quote from the book:

Subjects provide a convenient way to poke around Rx, however they are not recommended for day to day use... Instead of using subjects, favor the factory methods...

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