Rx-java Pass By Reference Or Pass By Value?
Solution 1:
I don't think I could explain Java's parameter semantics any better than (or even half as good as) the link you referenced in your first paragraph so I won't try. The main point is: Everything in Java is passed by value (i. e. copied) but with objects what is copied is not the object itself but the reference to the object. So in other words the reference is passed by value.
So with respect to your particular problem: Yes, if you pass a reference to a mutable object to some rx-java code that reference will point to the same instance of the object. If you mutate the instance then the caller code will also be able to see the changes because they were made on the same instance. That's because rx-java is still only Java and cannot change the language semantics on that level.
Without seeing the whole code I am unsure what could be the problem here... When are you checking whether mNumberSettingsNew
actually has the changes you were making in your doOnNext
? If you check that immediately after saveSettings(ns, mNumberSettingNew).subscribe();
your uploadAsync
may not have returned yet. You could try adding an actual Subscriber
in your subscribe
and check the result there.
On a more general note, I think you should try to avoid side-effects like this as much as you can when using rx-java. Your case - taking an input object, applying a set of (possibly asynchronous) changes to that object, and waiting for the changed output object - is a bit tricky, but I think it could be done with scan
. Maybe something vaguely like this:
Observable.from(changes.added.entrySet())
.mergeWith(Observable.from(changes.edited.entrySet()))
.scan(menuBeforeAnyChanges, new Func2<Menu, Change, Menu>() {
public Menu call(final Menu previousVersionOfTheMenu, final Change nextChange) {
// since I don't know of a version of scan that can return // an Observable you would I think you would have to adapt// your code in here to be fully synchronous - but of// course the scan itself could run asynchronouslyfinal newVersionOfTheMenu = previousVersionOfTheMenu.applyChange(nextChange);
return newVersionOfTheMenu;
}
)
This would take the original Version of the menu, consecutively apply all the changes from added
and edited
and /emit/ every updated version of menu. So you would not have any side effects but simply subscribe to that observable with a Subscriber<Menu>
and then take the last()
Menu and that would be the one with all changes applied.
EDIT: Oh, I just saw that there is another method called reduce
that does just that: first scan
and then last
or takeLast
.
Post a Comment for "Rx-java Pass By Reference Or Pass By Value?"