bobbaluba
bobbaluba

Reputation: 3884

What is the difference between a BehaviorSubject and a ReactiveProperty in UniRx?

What is the difference? To me they seem very similar... Why would you use one over the other?

Upvotes: 2

Views: 1860

Answers (3)

Guney Ozsan
Guney Ozsan

Reputation: 330

In addition to the differences mentioned in other answers is BehaviorSubject is kinda more encapsulated compared to ReactiveProperty. That is:

  • BehaviorSubject.Value is get only (but .OnNext(value) is still publicly available).
  • ReactiveProperty.Value is settable publicly.

Upvotes: 1

Ryoichiro Oka
Ryoichiro Oka

Reputation: 1997

They're similar in that they will both store the last given value & you can access them via the public property.

Difference is:

  • ReactiveProperty will issue an event if and only if the value has changed.
  • BehaviorSubject will issue an event whatever the new value may be.

In their C# code (which is open-sourced) ReacitveProperty checks equality of new value and last value; BehaviorSubject doesn't. In fact none of ISubject implementations checks the equality.

When should you use one over another?

I would use ReactiveProperty when I want callbacks only times the value has changed even though the substitution may occur more frequently. I would use BehaviourSubject when I want callbacks whenever the substitution occurred.

In most cases ReactiveProperty is more useful because generally you want to react to changes. BehaviorSubject has its own use although less common.

Other considerations:

  • ReactiveProperty.SetValueAndForceNotify() can be used to invoke callbacks regardless the equality.
  • IObservable.DistinctUntilChanged() can be used to (secondarily) introduce the equality check.

Upvotes: 4

Arthur Leandro
Arthur Leandro

Reputation: 9

Behaviour Subject is one of the implementations of the Subject class. It allows you to retrieve the last value that was pushed by using the outObserver.

Reactive Property is used to provide some sort notification when something happened, it's a simpler alternative to callbacks.

For example: If you want to chain a lot of methods to an action in your game for instance the movement of an player you can use this Rective Property.

If you want to get the last value that was pushed for instance to make a combo in a fighting game you can use Behaviour Subject.

Upvotes: -1

Related Questions