RxJava – Difference between Map and flatMap

mapvsflatmap-title

Introduction

If you’re an Android Developer you have most likely heard about RxJava and preferably plan on learning it (Lately I worked as a contractor for a big company where the android team has not heard of it yet, sigh). RxJava is the library when it comes to asynchronous and reactive programming. Before the typical way to do Async work was using Androids AsyncTask, Threading and a mix of Loopers, Handlers and Messages.

This usually not only endet up with a lot of boilerplate code (hello callback hell), but it was very easy to screw up and hard to debug.

We can use RxJava instead all of these things, enabling us to do complex stuff with a few lines of code.

There’s a famous quote of the founder C++, Bjarne Stroustrup,

„In C++ it’s harder to shoot yourself in the foot, but when you do, you blow off your whole leg.“

I guess this applies also to RxJava with its high amount of operators (see http://reactivex.io/documentation/operators.html for reference).

This Article is not about the basics of RxJava, I recommend you to go to vogella (https://www.vogella.com/tutorials/RxJava/article.html) if you’re not familiar with Rx at all yet.

As the title says this article is about the operators flatMap and map.

Scenario

For the purpose of demonstration let’s say we’re doing an API call and want to deserialize the JSON (you should not do this manually, there are libraries like GSON for this).

We’re calling a getUser (e.g. „/user/1“ where 1 Is the id) endpoint and receive a JSON which allows us to construct an instance of User

Map

The Map operator gives you the emitted data and allows you to apply changes to it. The resulted changes are being emitted then again.

userService.fetchUserJson(id=1).map { resultJson ->

this.userMapper.mapFromJson(resultJson) // the returned object here would be the user

  }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .subscribe ( {

// on success

view.displayUser(it)

},

{

// on failure

view.displayError()

})

What happens here is we’re doing the API call through fetchUserJsonId(id=1). Once the call is done we’re calling the map function which maps the resulted JSON to a User object and then emits that user object further down the data stream.

For the purpose of demonstration let’s say we’re doing an API call and want to deserialize the JSON (you should not do this manually, there are libraries like GSON for this).

flatMap

The FlatMap operator works almost the same as the map operator and can fulfill the same operations as the map operator.

Within the operator you receive the emitted data to which you can apply changes. However, this time the resulted data is not emitted directly but an observable of it is. In our scenario flatMap would return an object of type Observable<User> unlike only User, as in map.

This does not happen by magic, the specified return type of flatMap is a SingleSource. The compiler will force you to return a single

 

userService.fetchUserJson(id=1).flatMap { resultJson ->

Single.just(this.userMapper.mapFromJson(resultJson)) /* We’re forced to return a Single here. Returning just the user would give us a compilation error */

  }.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .subscribe ( {

// on success

view.displayUser(it)

},

{

// on failure

view.displayError()

})

Now if would we have an observable that is constantly emitting items (e.g. a MessageObservable for a chatting app) then flatMap would be emitting for every received item another observable, which you could subscribe to again and do another heavy task (like another http request).

Due to its asynchronous nature it is not guaranteed that all the received objects of a flatMap operator get emitted within the same order again (You have concatMap for this, however this is out of the scope of this article).

When to use flatMap and when to use map

What are the main differences between map and flatMap ? Let’s say our observable is emitting an object Tuna.

The operator map emits an object of Tuna while flatMap emits an Observable of Tuna (Single<Tuna> to be specific).

While map blocks the stream until the mapping operation is done, flatMap transforms the returning object into an Observable and grants us therefore another potential async operation. Because of the 2nd async task in flatMap, we’re not guaranteed that the items that were received by the observable are also emitted within the same order. If we were using our heavy calls inside map, then the map method would have to be executed synchroniously and block meanwhile other items that were emitted and want to go further down the steam.

This gives us a clue when to use which:

  • Whenever you want to do an offline operation (e.g. map an object Tuna to an object Bacon, verify data offline, check some fields) then you should use map
  • Whenever you need to do another async call (e.g. http request) and need your data to be updated instantly then use flatMap()

 

I hope I could put some light over this topic for you. If you have any questions feel free to use the comment section.

PS: You might have noticed that I used Kotlin in this language. Kotlin is an amazing language crafted by JetBrains. You should definitely check it out if you have the time as it is slowly but surely becoming the new standard of android development. With its language tools you save a lot of boilerplate code and thanks to functional programming you’ll have new elegant ways to write your code.  I can highly recommend this book if you want to learn Kotlin or read about it. What’s your opinion about the new rising giant?

Android Development – Memory Leaks and Garbage Collection

Note: Android runs on ART (Android Runtime) which is another runtime environment however GC works the same as on JVM.
How does Garbage Collection work? What is a memory leak? How do I avoid a memory leak?

You probably know that objects live inside the heap while references and variables live in the stack. In this post we will focus mainly on the heap. Objects inside a heap have different life spans due to garbage collection, which is very good, because fragments get created, destroyed and created over and over again (at least they should) and require the space to do so while other objects are wanted to live a little bit longer (a bitmap cache, a global state holder, a repository, etc.).

Many android developers I’ve met have heard of memory leaks but are not really aware of what they really are and how they are caused. Most of them know that a progress dialog could cause memory leaks if not handled and cleared properly. Despite knowing that, some don’t understand why it actually causes a leak. I have seen Developers setting a reference to a view / context in a static field, just to reuse it later for dependency injection, a sensor API, etc. This will always cause a memory leak if the view / context is not unreferenced after it is unusable (e.g. when the fragment is supposed to be destroyed).

If you don’t want your view (or any other object) leak memory, then avoid coupling it to an object which will live longer than your view. If you do it anyway, make sure you free your view from that reference once your view should die.

A memory leak is basically an object you are not using anymore but is still referenced. That object will live inside the heap blocking space for further allocations.

Structure of the Heap

The GC basically scans the heap and analyzes the objects. Unreferenced objects are marked for garbage collection (removal of the heap). However, the heap can grow very big and complex so it could be very inefficient to constantly scan all the objects inside the heap. How does this happen then?

Oracle has done some research and found out that most allocation of bytes happens in the early stage of an application and it decreases during during the life time.

Most allocated bytes have a short life span. Source: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

For this reason the heap has been broken down into three categories. The Young Generation, the Old Generation and the Permanent Generation. Additionally, the Young Generation has 3 containers: eden, S0 and S1 (we’ll talk about them in a second). The objects inside the heap contain an age, which you can think of as an integer that is being incremented after certain operations. The Young Generation contains the freshly allocated objects, allowing short living objects to be removed quickly. A garbage collection that happens here is called minor garbage collection. The Old Generation contains older objects, the ones that are already alive for a longer time (Repositories, Application Class, etc.). Your view should actually have no business here. A garbage collection that happens here is called major garbage collection. The Permanent Generation consists of meta data required for the JVM which is used to describe classes and methods.

The structure of the heap displaying the Young Generation with its sub parts, the Old Generation and the Permanent Generation.

The rhythm of the Garbage Collection

As stated above, all new allocations happen first within the Young Generation part of the heap, inside eden to be specific. Means all new objects land here first. While the first objects land in eden, the subparts S0 (Survivor 0) and S1 (Survivor 1) stay empty. Once eden is filled with objects completely a minor garbage collection happens which causes all unreferenced objects at that time to be kicked out of the heap. The objects that survived the garbage collection are the ones that are being referenced (and hopefully used by the application). The survived objects are being moved now into S0 (note that we are still inside the Young Generation part of the heap). Moving alive objects into S0 made eden empty again.

Once eden is full the GC gets triggered again, removing all unreferenced objects. This time, all referenced objects get moved to S1 unlike previously to S0. The objects inside S0 get their age incremented (remember, the integer?) and are then moved into S1. This is how the GC knows how old the objects inside the heap are.

After some time passed eden is full again and now another garbage collection happens, what now? All unreferenced objects are removed again. This time all referenced objects are being pushed into S0. All currently living objects inside S1 get their age incremented and are then moved over to S0, too. Now we have 3 different generations of object inside S0.

Once objects reach a certain age, they get promoted and moved into the Old Generation part of the heap.

TL;DR version

In the beginning eden S0 and S1 are empty.

  1. All objects are being put into eden
  2. eden is full and triggers a reference analysis on all objects inside the Young Generation Heap
  3. All unreferenced objects are removed (the actual garbage collection) and referenced ones are put into S0
  4. eden and S1 are empty, S0 has objects
  5. After a while eden is full with objects again and step 2 repeats
  6. Step 3 occurs now again, but this time all items are moved into S1
  7. All previously stored objects inside S0 get their age incremented and are then also moved into S1
  8. eden and S0 are empty, S1 has objects
  9. Time passes and eden is full -> Garbage Collection is triggered again
  10. All items get stored inside S0 and step 7 happens again, only with Objects inside S1 being incremented and moved into S0.
  11. Once an object has a certain Age it will get moved into the Old Generationstore. All the 10 steps are repeated over and over again

This flow of garbage collection is also called Generational Garbage Collection. The older an object gets, the further it gets promoted (Young Generation -> Older Generation). The older your object gets the more seldom it will be touched by garbage collection.

I hope I could have helped you with understanding what memory leaks are, how they happen and why you should avoid having your views referenced by objects that outlive it (e.g. static classes).

Flutter – Cross Platform Native Mobile App Development with Google (2018)

Note: this is the View of me, a Senior Android Developer, about Googles Mobile Cross Platform technology called Flutter. I recommend you to check it out.
Recently I was looking for something new besides Android Development. I felt like it was my call to grow as a developer. I taught myself some Skills in Backend Development (Python / Flask), which led to my current Situation where I work as a Backend Developer at a Startup. However, I do not want to give up mobile app development as this can be a very lucrative Skill. The Mobile dev area (especially in Android) is changing very rapidly. Noticeably there is this Framework called Flutter, which is developed by Google and seems very promising. With Flutter you can develop native cross Platform Apps. This is possible due the frameworks ability to render its own widgets instead of relying on the OS Elements. Flutter does a very great job at this and the development is very smooth and easy. The language is Dart, which is a very flexible, elegant and strong language. Besides Flutter, Dart is also used in several fields like Backend Development.
Flutter itself supports a very reactive style of Programming, which reminds a lot to React due its state-based architecture. Flutter development brought me the joy and fun I had back in the days when I started to code Android. Already learned skills like architecture design, clean code can be applied to Flutter development. Besides that you can create so called ‚channels‘, which allow you to write code extensions in Java/Kotlin and Objective-C/Swift so you’re able to hook some 100% ‚native‘ Code to your Flutter code.
 I am not too familiar with the fairly big framework yet, but I can tell you that I feel it easier to design according to material guidelines with Flutter than with Android Native, eventhough you have to design the UI via Dart Code. In fact, Flutter offers more in-built ‘material-ish’ Widgets (Views) than the Android SDK. I also feel like Flutter allows you to focus on the more important stuff on the App: Business Logic. You can always customize your Widgets if you feel like, but the in-built styling functions are already offering alot of styling-possibilities. You’d be amazed how easy it actually is
After all, Flutter is still in Alpha Stage and I feel already really good about it. We can be excited for the updates in 2018
The docs are very easy to follow ( official website )
I plan to make some flutter tutorials, especially ones that are easy to follow and understand for android developers, however iOS guys and programmers from other field will be able to follow, too (hopefully)
btw, Flutter has an amazing Reddit community where you can sometimes meet some people from the Flutter or even Dart Team. Check it out here

Android Firebase – Reading from Database

Hello Folks, how you doing? Today I want to tell you about Firebase. It’s relative trendy lately and though Google’s Codelab offers some tutorials and has an open Documentation it’s still misses some explanations. I’ll teach you how to iterate through your Database and access Data.
Firebase has been pushed by Google for a very long time! Recently it added alot of awesome new features to make app development easier for you, including Notifications, file Storage, Analytics, Remote Config, Dynamic Links, and more.
Alot people have been using similar Platforms like Parse.com for a long time. Personally I think it is the best Parse Alternative you can find, in fact I think it’s even more amazing!
However, for People that are coming from Parse or similar Platforms  it’s quite Different to iterate through the Database and read Data.
Firebase is using a NoSQL Database, therefore all the Data is in one Table which has a Tree Structure (Root -> Children) and you need to Index your own Objects. This Article is not about Data Structure, you can read more about structuring your Firebase Database here.

Let’s get started.
Firebase returns Data in JSON Format. The good thing about Firebase is, that it supports direct translation into Java Classes. Let’s say you have a ‘Child’  called “Houses” (A Child in NoSQL is equivalent to a SQL Table / Table-Entry ). You can create a JavaClass called Houses and directly convert the Data from the Database into a House Java-Object.

Here’s the Cooking Receipt to query through your Database:

  1. Initialize a FirebaseAuth Object. Auth is required to access the Database, if your App doesn’t support LogIn/Authentication then you can go to your Firebase Console -> Authentication and enable Anonymous Login, which authenticates the Appuser without having to login or pass any Form of Authentication
    FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
  2. Create a FirebaseDatabase Object
    FirebaseDatabase mDatabase = new FirebaseDatabase.getInstance();
  3. Create a FirebaseReference Object. A FirebaseReference is a pointer, that refers to a specified Child in the Database.
    FirebaseReference reference = mDatabase.getReference();
  4. Now here’s the thing. You need to manually point your Reference to the Child/Entry you want to read. Let’s say you have a Child called “Houses”, that contains many Entries. Take the Reference we created previously and set your Reference to “Houses”.
    Note: the in Step 3 created Reference points at the Root of the Tree, so we have to navigate through the Tree to the Child we want to access. In that case we want to access the direct Child called “Houses”. The Structure of our Database is Root/Houses/
    FirebaseReference housesReference = reference.getChild("Houses");

Now that we have the right Reference that points at our Child we simply need to add a ChildEventListener, which iterates for us through the whole Child and let’s us handle everything it finds:

private ArrayList<House> dataSet = new ArrayList<>();

/*the ChildEventListener iterates through every single Child of our Reference
and let's you access the Data.
*/
housesReference.addChildEventListener(new ChildEventListener() {

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

/*onChildAdded gets called when the Reference points on a Child of
our Houses Reference. In this cases ever
taking the Child of "Houses" and convert it directly into a Java Object
and add it to our ArrayList, which we can use for further Code
*/
House house = dataSnapshot.getValue(House.class);
dataSet.add(house);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {
}
});

Thank you alot, if you have any Questions you can leave them in the Comments.

Best regards,
Martin

JavaFX – Passing information between Controllers

I frequently see on JavaFX Groups (e.g. on Facebook) this Answer:

How do I transfer Information from one Controller to Another one?

Before we get to the Solution, let’s talk first about what a Controller really is.

A GUI, graphical user interface,  consists (most of the time) of several elements and various Windows.

In JavaFX every window has it’s own controller. A controller is the codebase behind the user application —->  it has all the logic.

Let’s say an interface has a  red button which substitutes a certain functionality. The use case would look like this:

1.User click button —-> 2.Controller launches custom method: buttonPressed()

The GUI catches the user Action (1.) and communicates it to the controller, which starts the method (2.).

Sometimes code has to pass information to another window or  get information from another window. There are  2 simple solutions:

  1. Create a new Loader
  2. call  the Method setLocation(YourFXMLRessource);
  3. use FXMLLoaders Method called getController();
  4. create a static Method getController(); in your class you wish to have wide Access.

Here’s a code Example

AnchorPage page;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/yourFXML.fxml"));
samplePage = loader.load();
YourCntrl temp = loader.getController();
globalCntrl = temp;  // this will be

The code speaks for itself. Now that you have created the first 3 Steps, here is the Code of the Method

public static YourControllerType getController(){
return this.globalCntrl;}

Now that you have created  a “global” getter for your Controller, you can call all your public Methods and public Variables from other Classes with

YourController.getController().customMethod();

 

Enjoy creating awesome Apps

 

 

Martin

JavaFX – Easy Way to save Scenes/Nodes as PDF

Recently I had a Customer who was dealing with Properties. He needed a Software that allowed him to create customized multi-paged Reports (static Layout, dynamic Content).

I told him it would be no Problem to do that, I designed a few basic Layouts and implemented them in FXML+CSS to an A4 sized Node. I added a Content Managment System which had access to all the Pages via it’s Controllers.

Programm is built. Project is done.

Wait, the customer wants to print,save and/or reuse the created Reports. Shouldn’t be a problem.

Reusage: I wrote a Class that contains all the content and  then I save it’s data in a XML File. Easy thing! There are many API’s that allow me to pull Data from XML Sources and fetch them.

Printing: JavaFX 8 offers a PrinterJob Class which lets you with a few lines of Code print a given node

PrinterJob job = PrinterJob.createPrinterJob();
 if(job != null){
   job.printPage(p);
   job.endJob();
 }

Saving: Now that part was tricky. JavaFX2 offered you the solution to screenshot your Node and save it via Streams as Image on your Hardware.

Pros:

  • Sounds simple

Contras:

  • Many different Implementations
  • Buggy
  • Render-Quality sucks
  • specific Content not reusable since it’s a whole Image

As you see, we have to find something new. I tried alot of different things and after a while I started to become frustrated because I could not come up with an efficient Solution. All the test-prints of the Reports looked like they came straight from the 90’s.

I took my compiled Programm to another Computer for testing on a different OS, when I clicked on Print a PrintDialog popped up. What I saw was the Solution: “write EPS to PDF“. Simple as that. I could save my Node directly to PDF. I implemented this in my code by adding a function JavaFX 8.0 offers, which lets you open your Print Dialog before doing the printing Job.

PrinterJob job = PrinterJob.createPrinterJob();
 if(job != null){
   job.showPrintDialog(window); // Window must be your main Stage
   job.printPage(yourNode);
   job.endJob();
 }

Voila. Problem solved.

Note: I used Windows 10. If your OS is NOT Windows 10 then you can install a Software that creates a Virtual Printer, which enables you to write your printing File directly to PDF (e.g. AdobePDF)

Programmers, IT Pioneers, Brothers and Sisters,
keep your Fingers healthy!

 

 

By the way, have you heard about JFoenix :  Java FX Material Design Library?

You should check it out here . If you want to have a Tutorial about it, let me know in the Comments