Explore the basics of RxJava and walk through real world examples. You’ll learn how to create a simple solution for complex problems, saving you lines of code and making your code more readable.
oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow. RxJava ← Reactive eXtentions ← Reactive Programing
for composing asynchronous and event-based programs by using observable sequences. It extends the observer pattern to support sequences of data and/or events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety, concurrent data structures, and non-blocking I/O.
callbacks ◦ Error handling sucks - Tuple?? ◦ The lost Context ◦ Composing multiple calls… oh no! ◦ Testing… yeah right… • Threading ◦ Very hard to handle concurrency ◦ Bugs are easy to create • IntentService ◦ Easy to use ◦ Updating the UI isn't trivial ◦ Concurrency…???
the Subscription object. The Subscription describes the connection between the Observable and the observer. As long as we are subscribed, we will get data emitted from the Observable. To stop the Observable from emitting more item, we just need to call subscription.unsubscribe();
public void onCompleted() { // YEY!! finished emitting all the integers } @Override public void onError(Throwable e) { // something bad happened } @Override public void onNext(Integer integer) { // we just got a new integer } }); Observable<Integer> myIntegers = Observable.just(1, 2, 3, 4, 5); mySubscription.unsubscribe();
data. Creating Observables • Just — convert an object or a set of objects into an Observable that emits that or those objects • Interval — create an Observable that emits a sequence of integers spaced by a particular time interval • Create — create an Observable from scratch by calling observer methods programmatically Transforming Observables • Map — transform the items emitted by an Observable by applying a function to each item Combining Observables • Merge — combine multiple Observables into one by merging their emissions • CombineLatest — when an item is emitted by either of two Observables, combine the latest item emitted by each Observable via a specified function and emit items based on the results of this function
that you apply to it will do its work, and will notify its observers, on the same thread on which its Subscribe method is called. The SubscribeOn operator changes this behavior by specifying a different Scheduler on which the Observable should operate. • Schedulers.computation( ) meant for computational work such as event-loops and callback processing • Schedulers.io( ) meant for I/O-bound work such as asynchronous performance of blocking I/O, this scheduler is backed by a thread-pool that will grow as needed • AndroidSchedulers.mainThread( ) (using RxAndroid) subscribeOn() and observeOn
{ @Override public void run() { // Do some cool stuff // on the UI Thread } }; //set a new Timer Timer timer = new Timer(); //schedule the timer, after the first 5000ms // the TimerTask will run every 10000ms timer.schedule(timerTask, 5000, 10000);
some cool stuff // on the UI Thread }); public static Observable<Long> interval(long initialDelay, long period, TimeUnit unit) { return interval(initialDelay, period, unit, Schedulers.computation()); }
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { GetPlaces task = new GetPlaces(); String check = mEditText.getText().toString(); task.execute(check); } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} @Override public void afterTextChanged(Editable editable) {} }); // get the list of predictions in an asynctask class GetPlaces extends AsyncTask<String, Void, ArrayList<String>> { @Override protected ArrayList<String> doInBackground(String... args) { ArrayList<String> predictionsArr = new ArrayList<String>(); try { ServerAPI.getSuggestionsFromGoogleAPI(args[0]); } catch (Exception e) { e.printStackTrace(); } // return all the predictions based on the typing the in the search return predictionsArr; } @Override protected void onPostExecute(ArrayList<String> result) { // update the list with new data } }
.debounce(300, TimeUnit.MILLISECONDS) .distinctUntilChanged() .filter(charSequence -> charSequence != null && charSequence.length() > 0) .map(charSequence -> ServerAPI.getSuggestionsFromGoogleAPI(charSequence)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnError(throwable -> { // show user something about this error }) .subscribe(strings -> { // update the list with new data });