RxJava (or Rx.NET) equivalent of ReactiveCocoa RACObserve

Given an arbitrary field of a Java object, I want to create an Observable that will observe this field and click a new result on the Observer each time the field value changes. ReactiveCocoa has a RACObserve macro that seems to do just that.

I want to know how to implement similar functionality using RxJava.

For example, let's say I had the following simple class:

public class Foo { enum State { Idle, Ready, Error } private State currentState = State.Idle; //methods that can change currentState } 

I want to create an Observable<State> that will trigger a new state in Observer every time something changes the value of currentState .

In ReactiveCocoa, it seems I will write something like the following (please excuse my pseudo Objective-C):

 [RACObserve(self, currentState) subscribeNext:^(NSString *newState) { NSLog(@"%@", newState); }]; 

How do I achieve similar functionality in RxJava? I think that I may need to move all changes in currentState to the setter, but it is not clear to me where I should then call Observable.create and how to pass the changes to currentState to Observer.

+7
reactive-programming rx-java system.reactive reactive-cocoa
source share
4 answers

ReactiveCocoa is actually more like ReactiveUI ( http://www.reactiveui.net ) than just Rx. And in ReactiveUI, you can use this.WhenAnyValue (x => x.PropName) to do exactly what you want.

+3
source share

I recently stumbled upon this problem, and I ended up using a PropertyChangeListener that returns an object when a property changes, see the following:

Update Listener:

 public class GameUpdateListener { public static Observable<Object> changed(Game game) { final BehaviorSubject<Object> subject = BehaviorSubject.create((Object)game); game.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent propertyChangeEvent) { subject.onNext( (Object)propertyChangeEvent.getNewValue()); } }); return subject; } } 

Some custom objects:

 public class Game { private PropertyChangeSupport pcs = new PropertyChangeSupport(this); ... public setSomeField(String field){ this.field = field; pcs.firePropertyChange("field", this.field, field); } public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) { pcs.addPropertyChangeListener(propertyChangeListener); } ... } 

Note:

 Game game = new Game(); GameUpdateListener listener = new GameUpdateListener(); final Observable<Object> gameObserver = listener.changed(game); gameObserver.subscribe(new Action1<Object>() { @Override public void call(Object o) { Log.e(TAG, "Object Changed"); } }); game.setSomeField("New value"); 

This will work fine if you don't need to instantiate the object again. Perhaps the solution to this is to create a local setter method and fix the changes there.

+1
source share

Since your question title contains "or Rx.NET", here is my suggestion (I don't know, RxJava, you may find something similar).

You probably have to use some kind of mechanism in the tuner. The standard way in .NET is to use the INotifyPropertyChanged interface. Then, by IObservable<T> events, you can create an IObservable<T> from this thread using Observable.FromEvent<TEvent, TArgs>()

You can find a really good example of what you want to do (.NET) here .

( Rob Foncesa-Ensor loans)

0
source share

I think you are after this Subject<T> . It implements IObserver<T> , so you can call OnNext(T) to start the new value, as well as IObservable<T> , which you can publish as public so that it can be signed.

If you need to miss the last value to new subscribers, you can use ReplaySubject<T> with a buffer size of 1.

Here is the basic implementation:

 public class SomeService { private Subject<int> values = new Subject<int>(); public IObservable<T> Values { get { // AsObservable prevents it from being cast back to Subject return values.AsObservable(); } } // Private; called by some internal mechanism private void SetValue(int newValue) { newValue.OnNext(newValue); } } 
0
source share

All Articles