Introduction to Reactive Programming with RxJava

RxJava is a Java VM implementation of ReactiveX, a library for composing asynchronous and event-based programs by using observable sequences. It is used for reactive programming which I like to refer as “programming with asynchronous data streams” or “programming waiting for something happens to trigger your code” or “programming to react undetermined events”

I’m gonna be straightfoward because this topic it is a well known case that you only learn by doing. Basically, this is how it works: A Subscriber subscribes to Observable, then Observable calls Subscriber.onNext() for any number of items, if something goes wrong here is Subsciber.onError() and if all finishes fine, here is Subscriber.onCompleted().

The foundations

Subscribers -> It listens to the events/items;

Observables -> It emits events/items - This class provides methods for subscribing to the Observable as well as delegate methods to the various Observers. It is necessary to trigger/listen/activate/start the observables otherwise they don’t do anything;

Operators -> Functions responsible for manipulate observables. It can applyed along other operators and it has seveval goals: creation, combination, transformation, filtering, etc;

Schedulers -> It manages the work with thread pools via Executors (java.util.concurrent);

Transforming Streams with operators

As it was said before, operators can manipulate observables. There are plenty of them and it is really important to know how/when use them. Here is the most popular RxJava operators:

Category Operator
Combination merge(), zip(), concat(), …
Filtering cache(), retry(), replay(), …
Transforming map(), flatMap(), concatMap(), …
Creating just(), from(), defer(), …

Further details

I totally recommend this talk by Jake Wharton, an outstanding developer and a reference in opensource projects. Another option is watch this video but don’t forget to practice, try to implement what it is suggested in the video.


Some useful GIT commands

Git is a version control system that is used for software development and other version control tasks developed by Linus Torvalds in 2005. As a distributed revision control system it is aimed at speed, data integrity, support for distributed and non-linear workflows.

git config

Sets configuration values for your user name, email, gpg key, preferred diff algorithm, file formats and more.

git branch -a

Lists existing branches, including remote branches.

git branch -d [branch-name]

Deletes the specified branch

git push origin –delete [branch-name]

Deletes the specified branch from remote

git stash

Temporarily stores all modified tracked files

git stash list

Lists all stashed changesets

git stash pop

Restores the most recently stashed files

git stash drop

Discards the most recently stashed changeset

git reset [commit]

Undoes all commits afer [commit], preserving changes locally

git reset –hard [commit]

Discards all history and changes back to the specified commit

git show

Shows information about a git object.

git grep

Lets you search through your trees of content for words and phrases. Example: git grep “www.google.com” – *.js


Model View Presenter (MVP) on Android

The MVP pattern allows separate the presentation layer from the logic, so that everything about how the interface works is separated from how we represent it on screen. Ideally the MVP pattern would achieve that same logic might have completely different and interchangeable views. First thing to clarify is that MVP is not an architectural pattern, it’s only responsible for the presentation layer . In any case it is always better to use it for your architecture that not using it at all.

Check this simplified MVP example for Android

Model: This is the well known model, present in many of the most common patterns and use cases. It represents the world we are working with, every real world element that our app should know how to represent. Both the View and the Presenter are aware of this model and they know how to use its properties and methods (the view doesn’t really need to know the model and originally shouldn’t but it’s a tradeoff that avoids the need to have setter methods for every field that we want to update).

View: As stated before, views are our Fragment and Activity classes that we are using, they are able to alter the UI as we need and they need to receive structured data from a source in order to populate themselves. This layer shouldn’t know anything else about the obtention of the data, we made it as simple as we could.

Presenter: This layer, introduced here, is implemented with native java code meaning it doesn’t need to know whether it’s used in an Android application or a web service. How does this work with our structure previously described? Easy, what we are going to add to each view is a simple interface which defines the actions that any view using a specific presenter would need to do in order to populate itself with the data it provides. The presenter is responsible to act as the middle man between view and model. It retrieves data from the model and returns it formatted to the view. But unlike the typical MVC, it also decides what happens when you interact with the view.

References

  • https://medium.com/mobiwise-blog/android-basic-project-architecture-for-mvp-72f4b33252d0#.gebnmc1rq
  • http://antonioleiva.com/mvp-android
  • http://hannesdorfmann.com/mosby/mvp/