What is the difference between using RxSwift's MainSchedule.instance and MainSchedule.asyncInstance within the context of observeOn?
RxSwift -- MainScheduler.instance vs MainScheduler.asyncInstance
Asked Answered
asyncInstance
guarantees asynchronous delivery of events whereas instance
can deliver events synchronously if it’s already on the main thread.
As for why you would ever need to force asynchronous delivery when you’re already on the main thread: it’s fairly rare and I would typically try to avoid it, but sometimes you have a recursive reactive pipeline where one event triggers the delivery of a new event in the same pipeline. If this happens synchronously, it breaks the Rx contract and RxSwift will spit out a warning that you tried to deliver a second event before the first event finished. In this case you can observe on MainScheduler.asyncInstance
to break the cycle.
What a great explanation! Thanks you so much! –
Nel
© 2022 - 2024 — McMap. All rights reserved.
asyncInstance
if you want the events to always be delivered asynchronously even if you’re already on the main thread.instance
may deliver events synchronously if you’re already on the main thread. – Unmerciful