Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!