Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava - The Intro and Beyond

RxJava - The Intro and Beyond

An Introduction to RxJava and RxAndroid as new way of writing program, especially with Android using example snippets. Evaluating the existing style of coding in comparison with RxJava.

Goals
1. Explaining the idea/philosophy behind RxJava (What is RxJava?) For whom this talk is addressing too. (What kind of programmer)
2. Concepts/Basics in RxJava (Observable, Observer, Operator)
3. Setting the environment in Android Project.
4. Examples with Observable/Observer, Disposable, Operator, CompositeDisposable
5. Schedulers with Examples (MultiThreading)
6. Pros and Cons (Using Coroutinues)

JItesh SUvarna

June 23, 2018
Tweet

More Decks by JItesh SUvarna

Other Decks in Programming

Transcript

  1. What are different types of Programming? • Imperative Style -

    Object-oriented and Procedural • Declarative Style - Functional and Logic Based
  2. Imperative Programming (OOPs) Each line of code is sequentially executed

    to produce a desired outcome. The imperative way forces programmers to write “how” a program will solve a certain task. Its all about the state !!
  3. Declarative Programming (Functional) Functional programming is a programming paradigm where

    you model everything as a result of a function that avoids changing state and mutating data.
  4. Functional reactive programming (FRP) FRP is the combination of functional

    and reactive paradigms(Ways). In other words, it is reacting to data streams using the functional paradigm.
  5. RxJavaSample This is the demo App that we going to

    refer throughout. Place your screenshot here
  6. What is Rx and RxJava ? • Reactive Extensions (ReactiveX

    or Rx) is a library. • It compose asynchronous and event based programs by using observable sequence. • RxJava is Java implementation of Reactive Extension. • You can create asynchronous data stream on any thread, transform the data and consumed it on any thread. What is RxAndroid ? • RxAndroid is specific to Android Platform with few added classes on top of RxJava. • Schedulers are introduced in RxAndroid like (AndroidSchedulers.mainThread()) for multithreading in Android Applications
  7. Big 3 O’s of RxJava • Observable: Observable is a

    data stream that do some work and emits data. • Observer: Observer is the counterpart of Observable. It receives the data emitted by Observable. • Operator (Transformation): Operators modifies the data emitted by Observable before an observer receives them.
  8. Additional Components • Subscription: The bonding between Observable and Observer

    is called as Subscription. (It is the implementation of Observer) • Schedulers: Schedulers decides the thread on which Observable should emit the data and on which Observer should receives the data i.e background thread, main thread etc (Multithreading).
  9. Basic Steps (Cont. ) 2. Create an Observer that listen

    to Observable. • onSubscribe(): Method will be called when an Observer subscribes to Observable. • onNext(): This method will be called when Observable starts emitting the data. • onError(): In case of any error, onError() method will be called. • onComplete(): When an Observable completes the emission of all the items, onComplete() will be called.
  10. Basic Steps (Cont. ) 3. Make Observer subscribe to Observable

    so that it can start receiving the data. • subscribeOn(Schedulers.io()): This tell the Observable to run the task on a background thread. • observeOn(AndroidSchedulers.mainThread()): This tells the Observer to receive the data on android UI thread so that you can take any UI related actions.
  11. All set !! Now you can tell you know RxJava

    / Reactive Programming in your INTERVIEWS
  12. Operator Operators in RxJava allows you manipulate the data emitted

    by Observables. There are two types of operators one that is used to create Observable and other ones to transform the emitted data. E.g. for Observable Creation create, just, fromArray, range E.g. for Observable data manipulation filter, skip, map, flatMap
  13. Operator (Cont.) Following are the operators that can create new

    observable.. • just() - operator takes a list of arguments and converts the items into Observable items. It takes arguments between one to ten. • fromArray() - operator creates an Observable from set of items using an Iterable, which means each item is emitted one at a time. • range() - creates an Observable from a sequence of generated integers. The function generates sequence of integers by taking starting number and length. • create() - operator is used to create new observable from scratch.
  14. Operator (Cont.) Following are the operators that transform the emitted

    data. • filter() - operator filters the data by applying a conditional statement. The data which meets the condition will be emitted and the remaining will be ignored. • skip() - operator skips the emission of first N items emitted by an Observable. • concat() and merge() - operator combines output of two or more Observables into a single Observable. • map() - operator transform each item emitted by an Observable and emits the modified item. • flatMap() - applies a function on each emitted item but instead of returning the modified item, it returns the Observable itself which can emit data again.
  15. HOLD ON !! I know its too much. We will

    get to the end together.
  16. Disposable Disposable is used to dispose the subscription when an

    Observer no longer wants to listen to Observable. In android disposable are very useful in avoiding memory leaks. CompositeDisposable When you have multiple Observables and Observers. Disposing them in Destroy one by one is a tedious task and it can be error prone as you might forgot to dispose. In this case we can use CompositeDisposable.
  17. Chaining of Operators When you want to get specific and

    unique output that cannot be achieved using only one operator, you can use multiple operator by chaining them for getting the output. Looking for example !! TRY IT YOURSELF
  18. Wait !! Its almost done. Fortune 500 company (GOOGLE and

    APPLE) are ready with offer for you.
  19. When you first hear, there is something like Coroutines that

    does same operation like RxJava / RxKotlin
  20. “ One can think of a coroutine as a light-weight

    thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine.
  21. Coroutines There are two functions to start a coroutine :

    • launch{} • async{} The launch{} does not return anything and the async{} returns an instance of Deferred<T>, which has an await() function that returns the result of the coroutine.
  22. thanks! Any questions? You can find me at @droidboy313 Repo

    Link : https://github.com/DroidLove/RxJavaSample