Mobile meetup – Reactive programming in Android @Talentica

Just another WordPress site

Mobile meetup – Reactive programming in Android @Talentica

so welcome to the reactive programming in android meter so just a brief introduction about me I’ve been in the industry for almost seven five years and most of my work has been in Hawaii domain currently associated with telling tikka software i joined like two years for i’m also one of the organizers for the moon my lambda per meter blue so during this my time i will be taking some polling so you might just honestly once said if whatever you want once it so nobody is i think you can answer honestly right so let your programming is actually it’s it’s a concept and there’s a steep learning curve but once you learn it so you will see that how easy it will make your where your lives as a developer especially as I am NOT a developer so the more I learn about it you know the more I feel that this is a library that should be included by default in even in hello world projects so I mean this I’ll consider this meter a huge success if you today when you go out in office meter just try out your program okay so how many of you are professional Android developers to develop Android apps that is to be expected this is ok and how many of you have heard of reactor programming and implemented it so this is actually very basics plus we’ll be covering some some of some intermediate and advanced concept mostly its basic and some of the concepts are very general one the older version of rx Java so it’s a significant improvement what is reactive programming and why we should be using recommend there are already so many models available out there so why we should be learning a new model and the new extension is Java Android the topic of this meter and then you will see how we can use in general retrofits reactive programming will see some of the operators there are so many operators in I cannot cover everything but like and then we’ll see how testing notes so during this makeup if you have any questions even just you can raise your hand and you can ask the question you can stop me right there if you wait till the end a synchronous programming paradigm concerned with data flows and propagation of change big word Caesar I know so in simple words it’s just programming with asynchronous streams of data data streams so you know it’s it’s nothing new it’s always been there it’s just somewhat we have actually you know they’ve modified it up is everybody familiar with observer pattern so it’s like you know it’s a combination observer pattern and iterator pattern plus some other things so first of all why do we need a synchronous programming so I’ll explain in terms of Android because you block the UI not only UI is evil when you block the UI especially in Android application what happens you get this I hope everybody is seeing this in your apps that you developed and this happens and what happens like you have said users and when the users are sad

this is what they do okay so now that we know why we need a signals programming so now it will make some sense because we don’t want to block the UI asynchronous programming means simultaneous operations at you you don’t want to queue the operations with one finishes and you do something else so easy just like you are doing multiple operations at the same time and as soon as the operation finishes you will get some sort of fall back so that you can put accordingly this is there anyone who doesn’t know who’s Jake water so if you don’t know Jake water then you’re probably a new Android developer newbie you know lying about being a nerd ever so this is I took this I’m an idea for means one of our stocks which by the way I mean you should all watch and anybody see me they stopped the Oxford so consider this is simple interface so the complexity arises when you don’t either say a signal as these are updating some remote database so what we do is we cannot just call these methods like this because we want to know if some error happens or it succeeded or not some happen we can take some actions accordingly right so we want some sort of all that so this is the popular way to do it like this is a kind of camera to do you have some listeners and the listeners will tell you if the callback succeeded or it failed so now now when you try to use user manager to set a name and you’ll do this you’ll pass actually the name that you want to set manager from and then you’ll pass the poll that successfully so if some healing happens would be like a network failure or 404 in the on failure method and success it will return the same user instance the user instance on the remote database now if you want to do elos so now this becomes complex the start becoming mountain say there are I just all I want to do is set a set name and settings and now I have to deal with four cases and there could be like if you mix in my there will be so many complex cases for simple operations and it becomes more complex when you try to you know when you try to execute this operation one after the other because the output from one API is dependent on the other then you will put this tapping inside the on success of this because I want this to execute only when settings at seams and put it there like this so this if you I don’t make you a part of the term this starts becoming baalbek hell this one alright second callback and if I want to do something else result of this fall back there will be third level in one of the apps that I worked on believe it or not there was ten levels of nested always ten levels it was going out of the page you know he cannot see even ones me so this is actually so this is not the end of the story now based on the EP I call the filtering we are based on the result we want to let’s say I bring the Uwharrie I want to update a text to you or just some updates on you guys name is sec alright so I’ll do this next inside of this just said the said the value is by this time we don’t know if the activity is alive or not so we have to add a check if destroy if selectivity is destroyed and you try to do this what will happen and it’ll crash that the kind of the plus there is another problem let’s say set name is called it got succeeded then set age range and its second some say ten seconds to complete the operation if it succeeds will appear so before Lex I said as soon as the setage this API starts to do its thing the easiest thing when you close the activity there’s the short term memory

leak here because the activity is destroyed and you’ll still of that activity so this is a short term only you don’t want that remember leak to happen and plus seals we have not specified any threads here and you cannot update a UI from a background thread like you need to pass a radical new I so we need to do this you need to pass everything in that on UI thread memory for fairly simple operation so this is where programming itself it’s a you know it’s a library for dealing with asynchronous streams of data so the programming is basically a combination of observer pattern that’s iterator pattern and plus some function support so is there anyone who has only buggy Java not in Coeur d’Alene or JavaScript or Swift or only Java so then you will find point shot support very important so it’s a combination of all this I wanted to write program here but it’s not function program because if you write if you write function programming here it starts becoming functional that you’re programming which is I mean so just some pumps or supports this is not the end of story there’s more to it it provides meetup so when I should be saying good things out here to programming which I mean even if this were this was not a really profound Meetup and give it a ski somewhere right I’ll say that as I say all these things either way so observer pattern if you’re not familiar with it there’s a source of data and if I made something I mixed some data and there are subscribers or observer words and they subscribe with the source and after subscription whenever it the source emits the data then is evil so this guy probably subside earlier so it has more data than number of beta so and if the subscribers if they don’t want to date that date I think of five with the source and the the data won’t be propagated to the subscribers now this is a standard observer pattern that was brought in by the Gang of Four but this observer pattern has some issues to issues to be specific one issue is there is no before the observer in the source okay when I say observer it’s the one who is observing and receiving the data and observable or source if this is the one that is emitting the data or sending me this movie for the source to inform the subscribers or observers but okay I am done I don’t have any more data if you’re not say okay I’m complete there’s no completion handler no no there’s no completion even if observer effect and if some happened here there is some error because obviously on something maybe could be anything there is no way to properly this movie for the source to propagate the error to the subscribers so reactive programming fixes this gap it bridges this gap this is what happens if there is a list of items or objects of values it wants to consume the value so to iterate over the entire list of items sequentially without worrying about the actual data structure that is used it could be array or list could be a 5 or 3 or put me anything so abstraction so the Hoover is consuming it it will just say you have any more bad news yes ok just give me the next value and you can consume that data you know policy and support so this this this is one thing that was missing from Java Java 7 had very limited support right it has some support there are lambdas and method references so what happens is there is a table right like that like there is a error list so you want to iterate over it we can iterate so now if we combine these two or the run pattern tells you that maybe I right now I don’t have all the data I may have some data

in the future you have a list of data already there’s no huge area and you can iterate over the items so you combine this data will be coming you know with time and you can operate you can you can use some functional operators on this data so functional programming I mean if you don’t know about it you probably see this itself if you see it the look at the model language is like Portland and Swift and any model language for that matter right so they all have function supporting some sort of function support and C in Java 8 also introduced function support the extension is a library for composing asynchronous events so if you want to know what you know who came up with reactive programming you can look it up I don’t have it in my slides no but we have Microsoft came up with collector programming and Eric miner from Microsoft the library for composing asynchronous and humans based programming programs using observable sequence so let’s see some of the big words yes but there are few big words like asynchronous even base and observable so asynchronous you already know what is this even based perform some operations when the event is triggered or you know based on based on some patience some even you can perform some operation asynchronous immutability asynchronous because it’s all the values of the array is not here completely the values will come with time and about because you cannot change it not exactly array it could be list also it could be a single item moves so reactive extension it’s available for okay you can sequel yourself right you pick the language observer it’s available there from you know see table unity java any correlating friends here Coeur d’Alene come on you should start using modeling language so you name it get option is there still them in a lot of active development is still going on so we can expect support for more language soon there’s no idea right there’s no where’s that boy dear so we use Alex Dawa and coordinating a few of the coordinate with other Java to implement a program business now by the JVM implementation object events Alex provide Alice Android it provides whatever is missing from onyx Java the Android specific things so we can use it with and idle development so from the official documentation order it says that you know you include a centroid in your griddle dependency with the origin yeah please come with our next job so because you know Anna’s Android releases are very few and it’s Java reason quite a lot and there are boxes and also you should use both of these together if you want to use reactive programming in Android there are a lot of big names about okay so let’s see some of the components of programming so there are basically three major components of vector belonging one is source of course something that I meant stated and it can be passed around between consumed directly and this is subscriber subscriber is the one who consumes the data that you get from the source it tells us you know where to process and consume so yeah these are the major components I mean once in will make it source and the subscriber source source will get some data from somewhere it could be from network database files it could be any operation it could be user touch event or a drag even XY coordinates so will not worry about

whether and this is the timeline and source what it does is it a mystery and you remember in observer pattern when I was talking about there are two gaps in observer has no source has no way of telling the subscriber that okay I’m exhausted I don’t have any more data so it’s there so if there is an error the observable or the source can inform all the SUBSCRIBE on okay I don’t have any more data so maybe you know you suffer the process and these are called exactly once when some error happens it will not call on next after that when on complete happens it will not call on next after that on next is how we pass the data so here we are assuming the data is some egregious three right so okay now it just doesn’t just omit the data depends on the type of source or observable and creating yes it is a subscriber so subscribe what it does is it will subscribe first it will subscribe to the service okay I know you will receive data and after subscription whenever this source emits the data observable receives it and observable update the UI or local dB this is different the data also broken and balanced under let’s say a subscriber does not burn any more data it could be in between it could be at any point of time so when the subscriber doesn’t dispose so in our example one this was unsubscribe so this was subscription so that will change to dispose in our example two so this suppose means this is having to no cancel the operation so say this is observable goods which is doing some network requests takes 10 seconds returns me some data and meanwhile my clothes activity user process activity in one of the slides that we saw so user versus activity this operation is going on right so what we can do is we can call this post and the operation X cancer and how shangela fix here is oh yeah before that this axe one level of abstraction right because this guy the subscriber I doesn’t care where the data is coming from it’s just getting a sequence of data from one Zalman oh yeah this guy gets it from is where we actually you know databases are best API is a remote databases now you’re sending your fix in this picture let’s see what that do notice what it does is you know it it parameter is concurrency so how many of you have done this this is the hand of you done this so if you have used Scotland okay I know most of you not use the cotton but if you use Portland so there are things like who routines there are co-routines rate which helps you be there basically you know cheaper sets which which we are in full control of unlike actual threads controls if you have done iOS development so there is something called GCD there Grand Central Dispatch it allows you to you know attach it extracts the concept of low-level threading and it allows you to perform asynchronously non-blocking i/o operations in a very simple in the very simple view is that even I’m aware of any threading libraries in Java or in Android that we use I don’t know I’m not aware it’s probably there but I’m not aware of it so here know the modern programming languages if you see each of these programming languages they have some sort of support for handling low-level threading so you don’t have to deal with creating threads and dealing with this threat management and synchronization I mean now what if I say I’m allergic to it actually seriously so pretty soon in like meters like this people will be saying cavemen used to do set equal to new trend because now there are some sort of rapper available everywhere in all the modern running raining so schedulers is what allows us to do this it also deals with threading concurrency synchronization and data structure oh yeah that was out of time finish so you can affect away these things so it’s it’s kind of like you know I don’t have to deal to civilization this is one big

part of when you like apps or you know applications you don’t have to deal with threading or synchronization or thread safety or fun current data structure addicts Java with will help you with this now it’s not magic you know it was not created by Dumbledore so magically becomes too late you have to learn it there’s some steep learning curve as I mentioned earlier but if you use it use it properly you will see more happy you this and all you need to do is it provides some factory methods so if you want to do some eye operation you can call settlers don’t I you got computation you have some computation related tasks if you have to create a new thread anyway you bring the new thread or if you want to you know you want control over how many number of threads that gets created it is optimized by the way for the based on the device and the number of control over it you can create your own executor pulling if you have them you can get your whole executor you can create any laws from there now schedulers so it fits so we can decide where it’s getting the data form if it’s getting it of a network it should probably be in this because you cannot update from the yeah we can so even buses are there even bus will not do with city and uh yeah I have this question after some slide yeah a lot of boilerplate there and you have to deal with a lot of things there so I don’t know there are some code types so you know these are generic terms like source and subscriber and schedule so we’ll Java specific types are specific type it is observe which like some of the main type this is not all once you start dealing with rxjava there will be there are more types that you can use so we’ll see what each of these are observer is something that you know the subscriber this the third is the second is the subscriber it receives data in the onyx fall back in the another callback and uncompleted if the the sources and it gives you or disposable so this is possible you can use to cancel the flows and to know that if somebody is subscribed or not this is source because you know there are various types of sources in our example there is observable there is single there is flowable so this is actually you this X signifies that this will become observable source or throwable source or single source so I am just what whatever is written here is only for observable so multi value reactive abstract class it could have multi values and it can it will emit the values enough or no orderly fashion in the unaged follow which is this you be not next fall of this so what it does is you can create this you if you want to create from scratch you want to decide what and there is a it takes a parameter when you try to create it create it takes a parameter which is observable on subscribe or observer observable singing along subscribe for single or unsubscribe which has subscriber meter and it gives you a meter so there we are we are in what will develop when we will call unmatched on complete or if some happens yes fulfilled all of them and then there is disposable it has like these two means this force or a disclosure so before you are sending some data it’s probably a good idea to see if it’s disposed the subscriber using this unsubscribe from the source and cancel if any flow that is in progress from view mobile consumer as you know function interface that accepts

a signal value so for side effects like do or next that we’ll see later or other so things like that so in their consumer is used we’ll just say it will just take one generate value so this is XML so it could be like observable emitter for observable and using emitter we can call on mixed these methods are available for so we can pass you can decide which value the pass between this side when to throw error what is my error and say all the data is done on computer runs formalization is for composing sources example it has you can see like it will be one parameter if you think of the one type and schedulers we have already seen this parameter is concurrency this is some it once the Eclipse so this is something that can act as both an observable as a subscriber and a source and observable and an observable observer will we not be covering this but if you have any questions we can ask physician you have to use it with caution so if you see this the source code for this subject lacerate the subject class extends from observable abstract class and it implements the subscriber interface the observer can access both if you use this properly it can ease out like some of the pain but yeah most of the times what happens is you don’t have to worry about not have to worry about something all right if you want to just start using Huggins Java Android application you know two three times and you’re good to go unit stop by the way that is the best way to dramatic sigh you know I actually when I started it I was going through the theories and I looked at I don’t know like 50 blocks and some videos je crois ten videos and other videos that had less than 20 views I’ve seen that also and then I once I started I started learning this I knew all the theory but I did not know how to implement it so once I started it right I got like you know I got creation wrong like 20 times after that I got a sign of it and now I can use it so this source what are the sort of the source is a variable not itself see these are the sources available direction observable which emits you know it can emit zero value or one value or n number of values and I can finish with on complete or it couldn’t values also there’s slow about single maybe a computer observable it ended zero and it permeates with success or exception it’s also possible without emitting a single value there was an error and it will call it will terminate with that section it’s also called flu Ebola is exactly the same as observable when it supports that pressure this is also back pressure is also one of the advanced concepts you will probably not be losing it but that pressure is something you know when next said there is a source which is emitting data at a extremely rapid pace and you cannot process the data as fast as the source is producing them but for example drag even you have a paint application that Euler is usually drawing something at a term memory user is one there are too many x and y-coordinates variables and you want to associate the data you want to process it then you want to if you are not able to handle this if your subscriber is not able to handle the data as fast as the source is emitting it so that pressure helps there so what it does is do not solve actually the issue but what it will do is it will it will defer the processing of some data to some point of time there it’s suitable strategy is like you can drop some data you can use some buffer so if you are dealing with some situation where the data emission is too fast you can use another reason to use is if you if you know what components compatible

with this observable the types single as a means in just a single item or another it has no completion indicator only successful failure may be in may be do you know options like in coddling there are malleable types you put a question mark then and ensure there are options it also has a question mark at the end so what X is no there’s some variable which is absolutely option string so it means 10 container string it can contain now also it’s an 11-time it can contain null or a string when you know what meaning is the same it can contain null or the actual value but not right now we’re talking about reactive either right so asynchronous but not right now some part in the future so except the active form of optional right now we don’t know if it contains a value or not but in the future it may contain a value or it may and this completely we can use this type of source when all we want is you know just successive phases we don’t care about response type at all and that time you can use computer any questions to lives taken there’s more to come in okay so I have one question now what you were asking okay so how many do you think this is true this reactive programming or source and subscriber it just a slight extension of observer pattern nothing more okay I said be honest so we’ve seen some things like scheduling is not there so we already know about a lot of things which will help us in actual development of this the BL power actually comes with reactive extensions here we have operators that can actually allows us to transform combine and manipulate and work with sequences of emitted items that is where the ring comes from that we’ll see now how to create of the robles yielding observables there are some methods here to create a movie if you want full control use create or if you for testing but like this is excellent for testing purpose if you are testing it so let’s say you have some sort of observable that if it’s operators like this this is nothing but in the number of

times this will take some actions accordingly so this inspection is not direct its that means a quiet interfaces so it’s more abstract so now in this active in demo I’ll show you so this demo how about it does is it it talks to some third parties and it gets images from let’s say I have an account in facebook it’ll get my Facebook images or Flickr images or this image or form site so it will get the images from there and it will show all the images in single page so now the problem is so now all of these they have different types of structures the actually image structure the metadata for the image this has some freaking different structure this has different structure I don’t want these structures new propagated here you know because donor this API change there’s a Facebook chain is the API I don’t want any things to be here I want this to remain independent so as soon as the data comes in so I got like a time data let’s say a list of image stuffiness of C I want to convert this into the list off this is my type that I’m using it I want to convert the session to this and this is independent of this only does the models are dealing with this right so now with this and in mind we can go ahead with this so a lot of Android developers what is the thing that model is you know just the Java model was no it’s not just the model that’s what a glass no it’s much more than that it deals with your entire data even better to get the data from local database API is all and if you’re using properly using NDB there’s no Android dependent component here no context no I’m not dependent component here so it easier of the testing so why people are using MVP pattern this because you know you want to fetch the new we can mock you can move this part in the move this part and you can test it easily these are transformation operators it will transform the type of leader on the observable itself so loop I can do this more okay there’s one more much I’m not so this more you can see you can read about it and by the way this is reactive X dot IO is a great source for reading all this documentation is there so per for what it does this this is the timeline and what buffer does is if I specify the buffer size of three in this example I’ve specified the buffer size three so now the observable that is the output give you a list of items when the buffer is full so first item receive it will not admit anything second item receive it not have it anything third item received now the buffer is full now it will emit a list of these three items same goes on so and one more

thing ever never you see this dodge this – this is the one completely and you see when you see a cross that’s one other river and these are taken from Maddox marbles another good site for learning reactive why this is why this colorful diagram so this is so you can know one great example I can think of with buffers yeah for this now this contains would be like this total of hundred twenty images now these 120 images right we don’t want to know everything you know want to apply pagination so I’ll use the buffer use the buffer with let’s say pagination of the buffer size of ten so you receive ten images you give it to me use it tell me – then you select map what it does is it will map to each item of the input observable and we create an observable out of each input item and so there are maxing if there are 20 items there will be 20 observables and then it will flatten it it will take all the values nope I hope is an example later there’s a institution there’s a college college has housekeeping stuff professors Dean’s the discharge staff students a lot of people right so now let us say we are getting the details of all these people and you want to just you know group it so we want to say if said student put it in shouldn’t do if it’s let’s char or D so what group I will do is if we give you this it will look the emitted items it will apply some sort of transformation in the input and then after applying the transformation it will emit those items as anybody has anyone used maps in Cortland in javascript in so you can do that but you you do it with tables the static types the study the single understand with you can do map it is known aspects also scan what it does is it successively it gives you the successive value so now it our next one it Lamech one it emits two input observable emits two so it will add these two one purse – it’s a you telemetry next in 3 2 3 plus 3 is 6 6 plus 4 is 10 and Osiris 15 it successively but each way in some cases it can be very helpful there is window what window does this does it look similar to buffer it’s creating the baggage of three and it’s emitting it it is actually very similar to buffer the only difference is buffer if you see it’s also creating a patches of three based on the buffer size and it’s emitting it emits a list of it will be a list of string integer or linear type for the pattern and this window it will emit actual observable so when you do this so now that since we have now we know that our asynchronous programming we know about how to create the source and we know about how to you know transform the operators sometimes what happens you want to filter it also and these are like different use cases so figuring operators there are many this should not take long because the names are so you can filter the values emitted by officer observable so basically of course we give you distinguishing the distinct values element that it will give you one of the level which is the

position position I don’t think on compute you see the filter based on some condition the condition here is greater than 10 so it will only emit given 10 reviews you can filter it out you can take the first value you can take a large value you can skip some initial values if you want if you want to take some just something first well as I say and reduce you can say it will take just you know specified number of values in between take last four masks just this last for you let’s move past one if you pass twenty to take last twenty will use this this if you think out practical purposes of this so there are gonna be things like I have some I want to show photos so now I can have one observable I could have one observable that is or reading from a local database I could have another observable that is getting the photos from the actual APNs now we want to use this more than one observable in conjunction with each other so now these are the combining of the robots you know I mean this is these are the ways to combine of the ripples you have the operators that helps us in combining observables so – what it will do is it will take the latest of the robot and the ladies home observable to omit the it could be if X is greater than Y I mean any condition if you do so now when one was emitted there was no very emitted from the second observable so this you know mind is to place an X plus y 1 now when 2 comes what is the latest of author will be a because b has not been emitted it’s a later part okay so if you combine with this it’ll emit this so similarly to P when he comes the latest one is to say to see to d23 comes the latest here is the V B 4 D Phi D because there’s nothing up to P so you can combine multiple robots or you can start with so this start with this you know we can start with a single value or start with an array or start with another observable you can I mean you can use all these things with started there is if what it does is it combines and do anything end-to-end means you know first when you from here first well do for me a combine it first when you’re miserable one first value for second value is one a 32b see what emitted a lot earlier than three but see these are the third items kilometers easy for the fifth there is no magic contact is when you want to emit all the values of observable one you want to emit all the values of observable one and once that is finished you want to emit the edges of the rover to in between because once it gets called it’s essentially the end of the observable sequences much it will emit as and when this is an emission happen climate that directly so a good thing all of these observable is all of these operators is you know they they essentially they they return the same books are same or similar type of observable performing transformational it will different type of the mobile so you can change these operators you can k these operators easily so now yeah think of it as like different apple and then emits oranges third it’s bananas and four summits strawberries now I can call merge operator you’ve seen this monument this is so now I can call this modulator so as soon as any emission happens in any of these it will emit that item so now it will merge all this into a single of the robot which is emitting all apples oranges bananas and

now man mattes it’s like you know you are not happy with the data you got the data but you’re not happy with the data so you want to transform the river okay sorry that came out wrong happiness you want to save Masai the data right you want to change the data in the way you want well actually data is happiness right it is happiness I think so well you want to convert the data so I’m going to take another time and yeah Matt helps you do that it will perform certain operation in all the items emitted by some observable so I don’t like strawberries so it’s filtered out struggle this is filter we are filtering out items they’ve already seen this I’m just showing you how to how to combine these operations how to change these operations so now once you get this you got to filter filter already and then we can reduce I’m not showing you the slides but what it does is it takes in a list of values and it will emit essentially just one value and that one value since we’re talking about reactive that one value will be it will be a maybe it’ll be a maybe because right now we don’t have any value but you may have somebody in the future so now you reduce it in the way the note saying right if life gives you observable of fruits create fruit salad so originally right there if you have two different observables it will not allow you to mercy on you can actually you you can do what Cuban flight map or even you can compose you can with a bitmap you can actually transform it transform it in the similar type and then merge it or you can use the object class in Java thank for some demos so before I want to show first so what it does is also in the code so now if it is there is a buffer of size 5 can you all see this or should I increase the form okay so there’s a buffer size of five so the dimension is happening one two three four five projects emitting the value only after of the buffer has food and in the last example you see the bench is not competed at the five limit is not reached here but it is emitting this because on the break or fall if you see I’m using so whatever we just said right from creational to combining of the rebels to transforming it’s filtering it so it’s a form area creating of the rubble forming a list of so this is transforming is observable and do one next these are with what I’m doing is whenever this is deceived so this if some of these look like Hawaii so it’s just you know makes the code

more concise uncompetitive note it again here on next so I wanted to test like you know you there is a subscribe happening here okay so from the start there is observable list of integers okay this is source get creating this is the source now we have decided which values will be emitted in batches of watt because using buffer so now here you are subscribing on yeah this is schedule essentially schedules over you so this will perform as the operations in the background okay and then what we are doing is in on next whenever it calls on next I’m just lending it out it’s a list of integers I’m printing on the ingenious and here what is happening is calling actually wants a bribe and you’re passing the observable here so this is the uncomplete here and it’s up to you actually you know you want to use one complete here you can call it here yeah you can call it here so I have not called it here and yeah there’s no one complete there’s no one complete printed anywhere because they have not used on country but if we essentially if we do this complete so it’ll print by autocomplete I don’t know if you can see or not so on complete you can write with subscriber with the observer here or when you call the SUBSCRIBE method if you see this returns we did this doesn’t return anything so boy so you get a disposable in on subscribe but if you’re calling on subscribe with a consumer it will return you want as possible so you can use it this possible and in the buffer essentially you can do you can write on complete operation here or with observer will be using this interface has a non-compete here and some I don’t happen to do anything we’ll call back here so this is buffer what else there’s one more thing that I failed to mention that is observers observables or sources there are two types there is hot observable this code of the minutes so what code of animals do is they will start entering the value as soon as there is at least one subscribe once they subscribe secondly subscribe it gets the heart of the Robel is you know it doesn’t care it doesn’t care if anybody is listening or not it’s still shouting it out it will keep hitting the values Oh so let’s do the first of the robot is completing put my thumb in and the second of the whole computer picture starts printing up at some point of time so you can see the first first observable you know it is printing after it is printing first some values because it’s subscribe earlier and the second observable then it subscribe it started receiving values after some time yes I am using interval here to create the observable so internal in a period of 1/2 millisecond a half second hundred millisecond and yeah well then when you call connect it will start emitting so it doesn’t matter this is a basically a publisher so what happens is and then when you call connect it will start emitting the values so doesn’t matter if you subscribe earlier when you call in the values and so this is the first subscriber and this column subscribe on this do one next and subscribe right so this is the first subscriber and after a weight period of two seconds there is a second subscriber so if you look at this and there is an initial weight I guess yeah initial weight of two seconds so for two seconds since it’s half second

emission so the values and we’ve called connect so initial soul values it will emit right in the first two seconds these are not captured anywhere but a subscriber comes in the picture after four seconds so 0 1 2 3 4 happen 0 1 2 3 happen on four on what’s this guy got subscribed and it started presenting value after the next two seconds this day I gotta subscribe instead of the citizens this is hot observable next seam add filter reduce the after the Kinect there is the one axis actually core next on next you know onyx just for emitters emitter will call on next and the the value that is passing on next is received into unmixed or if you’re writing observer of the observer interface if you dealing with there is on next there’s a mother itself you have natural to reduce let’s see what this does by different yeah so it is it emitted some values and it called on complete and we are reducing it so radios function is you know just add the value for 6 plus 3 9 9 plus 9 is 18 okay so now in this what happens is it’s creating an observable from one till twenty count is 20 so now this observable it contains integers from 1 to 20 now here if you see subscribe on schedules of computations do this is the background the computations and then I’m mapping it in p/j with 3 this is 1 so it is what it will do is it will multiply see all the items of the source so we after this we have like a table of three will have 3 6 9 multiples of 3 till 60 1 2 into 3 is 16 so this is nap and then I’m filtering it after map is done after multi is done I’m filtering it so just filter it out and give me everything that is less than 10 and yeah this is the subscription is happening when the next values received I’m printing the value on computers received I’m just printing complete and this is the reducer function what it does is it takes to my news and said in the middle so there are like 20 values you know it will give you this is how you write and on the success of the reducer function if you see this reduce right yeah see it returns maybe it will not because it essentially it will just submit one value and the reactive optional that we know maybe when this reduce you know this maybe succeeds what we’re doing is we’re just printing printing the reducer so now if you look at this this will make some sense so now we had 1 to 20 multiplied three with all the edges so it became three six nine twelve fifty now I filtered it with everything has been ten means it’s fine and the reducer what it’s doing is adding it up immediately so I mean if you look at this thing I mean this is you know this increases readability this is this whole in one go and I can understand what it’s doing do something on the computation to that do what in the source we have multiplied converted moment by multiplying three then filter it reduce right here I can see like anything that was metal to reduce our scan is again it’s simple it will just you know give you successive values so then verse 20 is 30 34 30 is 60 60 plus 40 100 100 plus 50 is on the 50 yeah so

it has initially the source is an observable that has one to five I’m using a just operator here this is very handy when treating of the robots and then mapping it multiplying it by 10 so one two five became 10 to 50 and then in the scan function I’m passing it X plus y so you know add all the values add success if you add the values and in the honest it will call successive the successive values are people so now then it will initially it will input it will emit 10 then 10 plus 20 is 30 then you subscribe on again this subscribe if you see this returns are disposable object so if I write this so this little X and let’s say for some reason I don’t want to use it right in this causes this pole so it will cancel the operation in between take a class are quite similar that says it if you want to see any of these just let me know as we good that zip it combines two or two observables yes same thing I mean as we saw in the slides so there are two observables you were saying different types I the two observables one is integers if you see this yeah this is observable of integers this is observable of soul B’s of the raw string and the resultant source I want to convert interesting so what yeah zip source emits source P and what is the function to zip it integer process so string plus integer will give you strings so now this come into source and I mean subscribe and this is no with filters you can specify a condition so every item will pass through the condition and if the if the output of this condition is true it will take that item otherwise skip will actually skip some items skipped no first five or something in the error so it emitted some items and then it will call it called error and there are actually you know one of the awesome things about reactive programming is its resilience its is its ability to handle so now let’s see what is happening here because if you remember in the slides I told that you know either it will call on complete auto never with one phone both simultaneously but see here error is happening I mean this is just as there are some strategies that you can use to deal with error yeah so there are see of the robots and then I’m using concat concat if you remember this like it will append all the robots in a sequential long complete them second observer along with and thought of them I mean it takes only two parameters one parameter so I chained it here so now this source it should emit one two three four and then exception and after see the exception happen it should never print out eyes examine it because it’s never going till there so here I’m subscribing it and here on error return item there is another function I could return this and when on complete happen just call on complete in on next just spilled the mutagen okay I’ll just

write it bring it and on so whenever happened you know I don’t want it to crash people return -1 and if you complete it successfully it’ll call uncomplete the items after there are not called if I remove this thing because it will say that you know you have to sew this in the you know handling handling there are so and so yeah nobody’s frantic next are stopping me from pushing the ribbons right so you’re pushing you’re saying let’s say four items for users got pushed and while pushing the while pushing the fifth use of this is salmon right so then you can retry you can you can’t buy a piece of thread you can buy every subset and I’m handling I’m not into it in the slides but there is some error handing it the rejected manifesto if you’re not this will this explains why do we need more reactive systems so do have a look at it reactive manifesto this one is the huh its disposal yeah catch and retry so there are these two operators of the earth there is retry and catch so what it does is a little bit different from this so what this sells you is you know you’re dealing with a list of items some error happen and then you switch over to some other of the you switch over this is not continue from there you switch

over from a different list no so that that you likely know when you receive things like if you have done some error handling things there like if you have like a site so you can handle that gracefully not abruptly you can do that and plus you since you are getting a call back in on on error also so from there some backup strategies that you can use to switch it this is error handling which was not in this race perfect with Java so now you can use retrofit with orange Java rx Java – okay in this slide wherever you see a text Java that means R is able to by default there’s not as your abundantly so in the interpret you’ve done this right so you create something that will give you a whole object and then you call what is thank you and you pass for you call this method followed and you it will incur it it will take and then once this is done you get a callback so again the callback that you get with accurate so if you want to perform multiple operations one after the other it will still become a callback hell and as I mentioned if the there’s a phone call right yeah when the user has your app opened some phone calls comes in your app is in the background the phone screen is in the top right or when the user presses the back button so at that time there’s no blockage of you or I but still since we are holding a reference to something activity that got destroyed you can get like window token exceptional definitely an exception unless you perform checking like if security is this one off this is the fuel retrofit thing you can use Adams never to adapter with retrofit there is a original to dr. with retrofit and while creating the retrofit filter right you just add the color perfectly for that Alex Chow Alex Java to perfectly you just end it and after this you can use you can convert this into this so we use retrofits so after that you don’t have to call and all because once you subscribe with it so you don’t have to call fetch you the removal of time also you get the order over item will subscribe acidity subscribe you start adding items so this is another thing I took from the jporten install it was in April so now what happens is we like to you know to deal with the removing edge to keep it Seymour database locally with the UN and that android logo is basically represents the OS so it can also give you a call back you know some phone call phone phone call is coming up and your back your phone is gonna that’s about to go in the background so we write code to deal with all this so usually what happens is you know I call some API give me the list of photos and as soon as I get the call that I update the local database and when that is done I update the UI okay the responses here until UI and when some things happen like Android OS is giving me some signals you know I have to deal with that also so with reactive programming based on what we have seen today what we can do is we can write code that reacts to changes so we can write code that this will you change yourself didn’t you so now this responsibility is remove from the code that we know we can write we can update to UI based on the series the database and you know user does something like you know set my name or get my username you can do these both of these reactively then whenever there is an API call and the response comes so it will contain the local databases yeah this directly interacts with the UI so the point is like the code written to manage all this is one don’t think of it as you don’t have to write code it’s like serverless architecture you know it’s not like there are no servers they’re all four there are servers and just that you don’t have to deal with the complexities of managing saying yeah only there’s no code the code is always there but you know all we are doing with the code is you’re just these things components together so let me show you one demo when you drank testing with rxjava it

provides text of the world subscriber institute you can use this protester and it provides like awesome methods when you want to assert with a single value or multiple values for an observer so testing becomes very easy in Java and you already know that you’re creating observer is so easy with in this corner observable not observable observable we keep going up the robot create to create manually observable just to just get observable that emits one value there are so many make effective methods that will help you so I created this sample app the code is available on github okay has anyone used dagger so this food is basically this sample app oh sorry let me just flip the screen no no I cannot forget the screen we’re having some chocolate ice cream so I just do the demo in the emulator as soon as it is up problem with the port I guess okay this will do so meanwhile when starting do you still think that it’s just a slight extension of the Ripper it is much more than that okay started I don’t know why is everything black so this is is implemented this okay so this is getting photos from excited and I have public photos for public and private photos and from this so here if you deny that at one site you will see a couple of things first these are all albums second since it’s public right so it will show you the total number of views on values so this album has two views and five images there’s a friend’s album it has three views and I think seven images okay Avengers has got zero views and Game of Thrones two views okay cartoons it was 320 views in Simon images so this there are some still some loose ends but you know a few zombie pattern so as soon as the page is opened it will it will call this API the model so basically it’s not get because it’s push-based right this is Pushpa so now once I click on any of these albums it will open the act album and it will show me the images I’ve used some coordinate things here so these it will show me the images of that that she will goes with Spidey costume director very similar right should not be with Captain America and yeah there is a filter functionality

here so if you think of it it is 327 to use it is adding up all the views of each albums and it is giving the total count right there on the top it is adding up total images in it is giving me a total image found right here so now what I’ve done is abuse Matt filter reduce and other reactive things I will show you the code in something and as soon as I start filtering you can see the number of views and number of edges got reduced based on the filters whatever is available like now based on this building I just say friends okay so this is this criteria meet just with friends and Game of Thrones so 3 plus 2 5 euros and it has 7 plus 8 15 images that’s and when I click back again so this thing that cop is number of this is reacting to some emails the filter is done observable gets to know that I need to filter something and it filters receiving and page on that filter it is doing some transformation with math and then it’s doing with use and after the reduce it gives me one single value and that single value is actually a single Java object that contains two well news total views and total images and I’m using that to update this so you see the what’s happening is that so it’s it’s the system is becomes even reactive will show you some food so now I’m going to ask you one question after I show you this quote that is readability question how is the readability good bad or ugly concise Michael but okay so here let me show you where the observable is coming from first of all and there is an album of the robot and I will see that later where is this coming from this this is giving me the list of something called pixie alum that is that is the model that I used and you know this mean you are related to something else so I’m just transforming it into this and using them so by the way because it’s MVP and the logic is in the data layer in the model layer right so this is simple logic that if network is available get it from the network network is not available get it from the local database I’m using my object box it’s still in beta about second so here next the network is available so what we’re doing is pulling the hems your API and FET user alums and it’s you know passing from user meaning and gives it gives me and observable of this user albums here as I mention since I’m using the rxjava to a doctor I can intervene right of the Rupa’s here so now based on some authorization and user name it is giving me the observable of this object this is specific to the imager side so now if you just want to see this thing every service has its own customisation right so if you see this for your class

so it gives me a status and is it successful or not a boolean and in the beta field it gives me the entire agonist yeah oh yeah this is the one so this again and itself and stators and all the elements the user albums are inside this the details of they’ll go judge the details of them there are no photos here right so now if you see and if I use observable I could have used singing along no signals because essentially there is only one object but what I want to do is I want to do I do not even operator so what I did was I transformed this observable which makes a single item into an observable that emits a list of these albums so now yeah this will give me an observable which emits this I use flat material if you remember flat now what it does is it will take each item from the input of the robot and it will convert it will use each item to create it own observable and it will flatten everything and it will give you one of the robot that emits all the values from all the so see else we have got only one value here so what I’m doing is I’m getting this and yeah it is giving me an album list it has a long list because it that’s actually the data part so it is giving me this I’m just turning it so now basically all the items of the album it will give me in a in a form so now I have got an observable of the user albums anymore yeah the album not the user album user normal user album is something that contains all the elements plus the status plus the boolean success and this contains only the data part I converted into an observable of this item and now I’m mapping as I mentioned it has this is the this is the polio that that is giving me I don’t know we use it so I converted this into something that I will use this is pulley that I created so if you look at this map what it’s doing is it’s taking my album there is an adapter I created so you pass in the imager model it will give you it will give me it will return to my water so now my got not leap up this created using this and then here this is the data based thing I am also what I’m doing is I’m saving this data I might open tables save it in local database remove it for duplication is there and I am using buffer here at the site size of 20 I’m using a buffer so now if you look at it every time somebody calls fetch albums every time anybody can call fetch alone right there could be photos in the first page in some other page some management field so anybody calls stretch albums this will update my local database and if network is not there it is reading it from the local database converting into the same type of observable it is literally so the presenter but it does mean that we call stretch albums it will call fetch albums but know does not know the database from from actually the server and it gets an observable of list of pixie albums Tracy so this is observable pH and I’m using observable or create here you can notice or creating if retrofit is a mean of the robot now once this is received I want to process each album so I can extract out the cover image so I can show the cover depth using album cover and also what I want is the number of images the number of views so now if you look at this so now we got an object of album observable now just look at it the how it increases the readability well what I’m saying is subscribe it on shade illusion are you basically i abound threats for missing on corporations everything written after

this will happen in a synchronous manner so everything written after this will be only mean so just looking at it you know and I’m most willing composed so compose what it does is it transforms observable one type to other day now here essentially now it’s similar so it takes an observable of pixie heirlooms it gives me an observable of states holder that states holder is the class that is holding number of fuels and number of images so it gives me observable of this I’m at that and in the map I can I’m creating this stats folder and returning it so now after this map is done it will give me an observable of all the other over lost this steps holder and I’m reducing it so reduces based on what just setting the album ID setting the number of views adding the number of views here and adding the number of images here because I want to show total number of changes and total number of views and after reducing it I’m just you know this is coming this so this up this will apply map and reduce that will give me the reduced what reduced from the statistics of the user legumes then I read an observer one method because this will I think switch down to computation thread switching to computation thread so everything written after this will be will happen this map and reduce will happen in a computation thread and this is a very handy method because reduce means if you were maybe I showed you reduce will give you maybe one object or it may not contain amount and you can just call maybe and there is a requirement for the compose method so we did that okay so now once you have the stats I can just call subscribe and this is subscribe this takes consumer I guess yeah consumer it yes it just takes whenever there is a 1 value thing right so in our example we use consumer it will give me the stats that was resultant of this operation this operations this is the sketch folder and I’m following Bureau updates checks with the joysticks so now I’m just after looking at this perform all the returns and the manner happens you want to handle hmmm you can handle the error here and observe because yeah what we’re doing is essentially you’re passing a long list so that you can get the data you don’t render the image based from the URL so that we’re doing in the middle after that but converting from you this to dribble dribble this directly you know this should actually this should give me a observable of single albums but since it’s not getting and converting into from week level at the model itself composing it on observing on the main thread and this and now this gets more interesting when you look at the filter thing if you look at it it looks complex right but it’s so meaningful here you are saying you know search of the world when you start searching it will call this function subscriber signals of value so to perform the operations in the background so filter it filter it based on the text that I been taking in the text box in the search box filter it and this is the serve this is my condition so for filter there’s one condition get rid off also this title contains the word or description contains that word or the provider contains that word you return us true otherwise and then when we have to update the view we are saying you know observe it or miss them so frequent thread switching switching is happening here initially it’s on a diagonal then do all this after this update on us there then this compose will actually switch to computation and computation thread will compute all the total stacks of the of the user and then we’ll again switching back to be inside and then you’re serving and you are

getting this it so let me show you this thing no one complete is called you know like when you are using a traffic light so it is giving off the volkl so it is calling on computer on the data is that and when in this example and I’m using the local database and network is not available here here I’m going to UM commit myself here so on complete will be whenever I search then search is done right so I am type F when I type F it will give me all the album’s that starts with F or contains ever realizes the condition it will power on computer that I type F a it will again do the same things it will cooperate you can change this according to your requirement here I’m printing it I’m printing the thread name okay so let me do the search again and we’ll see what what gets printed so I’m typing just tapping keyboard so you can see what is happening here is filtering is happening in some schedulers some cash shudders and updating in the album that is happening in the main thread MapReduce is happening in a computation thread operating album in the main thread again maybe reduce in this switch threats so easily I never knew this was possible just write this whatever is written after this will be on the main thread unless it is it encounters another observable like right here compose no it will not be computed in thread because since compose what is doing it is just transforming of the robot I have written here computation thread I can remove it certainly it will do it whichever so this this is filtering and one last thing I want to show is okay cannot show it from this because you know this port is not working I mean this port is actually currently busy and recording purposes so when you open an album there is a yeah this is I’ve also added a functionality to upload photos okay I’ll upload from here and I’ll show it there because I cannot approach from the genymotion almost a mucho so I’m putting this album the APA loading here by the way do we know that you can use rec trophic top road images also not just stressed EPA calls things alone this is the code I want to upload the image this is specific to this service with requires title description you know okay successful okay so in six

images it was five ugly this is a product just know so in the upload thinks what happens is like his example was the ready he said we want to say poses a list of users what we want Florida so now this is happening here when it gives me this is the image upload this is a main upload API you choose the file it will upload it and I chose a file and it gave me this this response so it is giving you the data of the image that got supported with success and skaters that is fringe scholars again success as true status is 200 now if you want to update it did something based on the status then you can use this if you don’t want to do this what you can do is you can use completable computable shows what it does is it will just tell you if the operation completed successfully or failed you can use that I’m using a single because it’s giving me a single menu so you can use that as well that was testing so that’s all I had for today thank you guys any questions yeah and the the demo app is available here I will be making some more changes in how Java or how does this into this yes so Flickr has some other pose right so it has a different structure and also either current between what is happening you know this is kind of very general it will tell you which it belongs to belongs to this service so in this what I’ve done I’m telling you this polar glass there’s no switch service it came from what are the user name of that service user sixteen oxen and the URL the table or the number of views the number of images so you can basically map this with everything no not mandatory so the question is asked is if I’m using retrofitted in the client does the package knows you need to be reactive that’s a question right there’s the questions on answer is no but yeah I mean you can use them separately but it’s not mandatory it’s a simple it’s a regular API also you can make your new question is out out of the climb get to know something except not know that doesn’t happen that will not happen if something changes in the remote server our client will not come to know okay something has changed magically unless you’re using some sort of right so in the if you if you remember the Android things the remote server changes and it’ll update the local database so that will not happen automatically if there is some API that is calling so I’m using an API that is getting some data updating the UI so one of the side effects with that API is it will update the local database whenever that remote database changes it will not give me unless you are using things like that and especially I mean I have not exported much but if you use it with the architecture components there is something called live data it’s awesome this will give you some changes live data will give me actually give me the changes this has changed what is the

change in run there are some things like that man has become much more than with what with rel me okay nice to use help you can use them you’re green down this what I’m using is green doubt it’s my optic boxes from window so then the any feedback form please right next meet associations and here we also have a slag group so if you are part of it you want some invitation so please mention your email address also in the forum so I’ll send your invitation by today or tomorrow yeah you can use it though since this is everyone probably with I think it’s called numpy and pandas level mean for data processing and this thing’s big Gator things data binding yeah and like you talking about mvvm sort of thing so it binds the data so it will just have the data model update it with the view right so your fills in like you know this is this is not just one thing itself it’s a broad concept it’s in a lot of things it’s not just about updating the data it’s all you know adding side effects and resilience and readability so you can use this with mvvm as well so while transferring data from the source of the data from there you are updating the model the view model these are some references that are used if you are serious about learning rx Java I’ll share this so these are some good resources there’s too much to note like so I’ll share this beauty and before you leave there is a snacks outside so please happen before leaving and if you have like not reacted to this if you have any other questions we in in meetup so how many of you actually coming in for the first time ok lot of you so what we usually do is after the beta bill is done we usually as a community we try to solve some issues that you have not necessarily to the meter that we’re having so if you have like any questions you can ask and the rest of us can gather and maybe we can try to solve your issues that you have you’re facing you