rx java - Understanding RxJava: Differences between Runnable callback -
i'm trying understand rxjava , i'm sure question nonsense... have code using rxjava:
public observable<t> getdata(int id) { if (dataalreadyloaded()) { return observable.create(new observable.onsubscribe<t>(){ t data = getdatafrommemory(id); subscriber.onnext(data); }); } return observable.create(new observable.onsubscribe<t>(){ @override public void call(subscriber<? super string> subscriber) { t data = getdatafromremoteservice(id); subscriber.onnext(data); } }); }
and, instance, use way:
action1<string> action = new action<string>() { @override public void call(string s) { //do s } }; getdata(3).subscribe(action);
and callback implements runnable
:
public void getdata(int id, myclassrunnable callback) { if (dataalreadyloaded()) { t data = getdatafrommemory(id); callback.setdata(data); callback.run(); } else { t data = getdatafromremoteservice(id); callback.setdata(data); callback.run(); } }
and use way:
getdata(3, new myclassrunnable()); //do in run method
which differences? why first 1 better?
the question not framework paradigm. i'm trying understand use cases of reactive.
i appreciate help. thanks.
first of all, rxjava version more complex needs be. here's simpler version:
public observable<t> getdata(int id) { return observable.fromcallable(() -> dataalreadyloaded() ? getdatafrommemory(id) : getdatafromremoteservice(id) ); }
regardless, problem present trivial there no discernible difference between 2 solutions. it's asking 1 better assigning integer values - var = var + 1
or var++
. in particular case identical, when using assignment there many more possibilities (adding values other one, subtracting, multiplying, dividing, taking account other variables, etc).
so can reactive? summary on reactivex's website:
easily create event streams or data streams. single piece of data isn't important, when have stream of data paradigm makes lot more sense.
compose , transform streams query-like operators. in above example there no operators , single stream. operators let transform data in handy ways, , combining multiple callbacks harder combining multiple
observables
.subscribe observable stream perform side effects. you're listening single event. reactive well-suited listening multiple events. it's great things error handling - can create long sequence of events, errors forwarded eventual subscriber.
let's @ more concrete example has more intrigue: validating email , password. you've got 2 text fields , button. want button become enabled once there email (let's .*@.*
) , password (of @ least 8 characters) entered.
i've got 2 observables
represent whatever user has entered text fields:
observable<string> email = /* figure out */; observable<string> password = /* , this, */;
for validating each input, can map input string
true
or false
.
observable<boolean> validemail = email.map(str -> str.matches(".*@.*")); observable<boolean> validpw = password.map(str -> str.length() >= 8);
then can combine them determine if should enable button or not:
observable.combinelatest(validemail, validpw, (b1, b2) -> b1 && b2) .subscribe(enablebutton -> /* enable button based on bool */);
now, every time user types new either text field, button's state gets updated. i've setup logic button reacts state of text fields.
this simple example doesn't show all, shows how things lot more interesting after past simple subscription. obviously, can without reactive paradigm, it's simpler reactive operators.
Comments
Post a Comment