Learning RxJava - Second Edition

Book description

Updated with the latest Maven coordinates, Java programming features, and API changes, this book is your guide to solving problems in writing asynchronous and event-based programs

Key Features

  • Explore a variety of tools and techniques used to solve problems in implementing concurrency and parallelization
  • Learn about core operators in RxJava that enable you to express your code logic productively
  • Apply RxJava with Kotlin to create responsive Android apps with better user experience

Book Description

RxJava is not just a popular library for building asynchronous and event-based applications; it also enables you to create a cleaner and more readable code base. In this book, you'll cover the core fundamentals of reactive programming and learn how to design and implement reactive libraries and applications.

Learning RxJava will help you understand how reactive programming works and guide you in writing your first example in reactive code. You'll get to grips with the workings of Observable and Subscriber, and see how they are used in different contexts using real-world use cases. The book will also take you through multicasting and caching to help prevent redundant work with multiple Observers. You'll then learn how to create your own RxJava operators by reusing reactive logic. As you advance, you'll explore effective tools and libraries to test and debug RxJava code. Finally, you'll delve into RxAndroid extensions and use Kotlin features to streamline your Android apps.

By the end of this book, you'll become proficient in writing reactive code in Java and Kotlin to build concurrent applications, including Android applications.

What you will learn

  • Discover different ways to create Observables, Observers, and Subscribers
  • Multicast in order to push data to multiple destinations and cache and replay them
  • Express RxJava idiomatically with the help of Kotlin features such as extension functions and data classes
  • Become familiar with various operators available in RxJava to perform common transformations and tasks
  • Explore RxJava's reactive types, including Flowable, Single, Maybe, and Completable
  • Demystify Observables and how they express data and events as sequences

Who this book is for

This book is for Java developers who want to leverage reactive programming to develop more resilient and concurrent applications. If you're an RxJava user looking to get to grips with the latest features and updates in RxJava 3, this book is for you. Fundamental knowledge of core Java features and object-oriented programming will assist you in understanding the key concepts covered in this book.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Learning RxJava Second Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Run code examples
      3. Download the color images
      4. Code in Action
      5. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Foundations of Reactive Programming in Java
  8. Thinking Reactively
    1. A brief history of ReactiveX and RxJava
    2. Thinking reactively
    3. Why should I learn RxJava?
      1. What will you learn in this book?
    4. Setting up
      1. Navigating the central repository
        1. Using Gradle
        2. Using Maven
    5. A brief exposure to RxJava
    6. RxJava 1.x, 2.x, or 3.0 – which one do I use?
    7. When to use RxJava
    8. Summary
  9. Observable and Observer
    1. The Observable
      1. The workings of Observable
      2. Using Observable.create()
      3. Using Observable.just()
    2. The Observer interface
      1. Implementing and subscribing to an Observer
      2. Shorthand Observer with lambdas
    3. Cold versus hot observables
      1. A cold Observable
      2. A hot Observable
      3. ConnectableObservable
    4. Other Observable sources
      1. Observable.range()
      2. Observable.interval()
      3. Observable.future()
      4. Observable.empty()
      5. Observable.never()
      6. Observable.error()
      7. Observable.defer()
      8. Observable.fromCallable()
    5. Single, Completable, and Maybe
      1. Single
      2. Maybe
      3. Completable
    6. Disposing
      1. Handling a disposable within an Observer
      2. Using CompositeDisposable
      3. Handling disposal with Observable.create()
    7. Summary
  10. Basic Operators
    1. Conditional operators
      1. takeWhile() and skipWhile()
      2. defaultIfEmpty()
      3. switchIfEmpty()
    2. Suppressing operators
      1. filter()
      2. take()
      3. skip()
      4. distinct()
      5. distinctUntilChanged()
      6. elementAt()
    3. Transforming operators
      1. map()
      2. cast()
      3. startWithItem()
      4. sorted()
      5. scan()
    4. Reducing operators
      1. count()
      2. reduce()
        1. Boolean operators
          1. all()
          2. any()
          3. isEmpty()
          4. contains()
          5. sequenceEqual()
    5. Collection operators
      1. toList()
      2. toSortedList()
      3. toMap() and toMultiMap()
      4. collect()
    6. Error recovery operators
      1. onErrorReturnItem() and onErrorReturn()
      2. onErrorResumeWith()
      3. retry()
    7. Action operators
      1. doOnNext() and doAfterNext()
      2. doOnComplete() and doOnError()
      3. doOnEach()
      4. doOnSubscribe() and doOnDispose()
      5. doOnSuccess()
      6. doFinally()
    8. Utility operators
      1. delay()
      2. repeat()
      3. single( )
      4. timestamp( )
      5. timeInterval()
    9. Summary
  11. Section 2: Reactive Operators
  12. Combining Observables
    1. Merging factories and operators
      1. Observable.merge() factory and mergeWith() operator
      2. flatMap()
        1. flatMap() with combiner
    2. Concatenating factories and operators
      1. Observable.concat() factory and concatWith() operator
      2. concatMap()
    3. Ambiguous operators
    4. Zipping operators
    5. Combining the latest operators
      1. withLatestFrom()
    6. Grouping operators
    7. Summary
  13. Multicasting, Replaying, and Caching
    1. Understanding multicasting
      1. Multicasting with operators
      2. When to multicast
    2. Automatic connection
      1. autoConnect()
      2. refCount() and share()
    3. Replaying and caching
      1. Replaying
      2. Caching
    4. Subjects
      1. PublishSubject
      2. When to use Subject
      3. When a Subject goes wrong
      4. Serializing a Subject object
      5. BehaviorSubject
      6. ReplaySubject
      7. AsyncSubject
      8. UnicastSubject
    5. Summary
  14. Concurrency and Parallelization
    1. Why concurrency is necessary
    2. Concurrency in a nutshell
      1. Understanding parallelization
    3. Introducing RxJava concurrency
      1. Keeping an application alive
    4. Understanding schedulers
      1. Computation
      2. I/O
      3. New thread
      4. Single
      5. Trampoline
      6. ExecutorService
      7. Starting and shutting down schedulers
    5. Understanding subscribeOn()
      1. Nuances of subscribeOn()
    6. Understanding observeOn()
      1. Using observeOn() for UI event threads
      2. Nuances of observeOn()
    7. Parallelization
    8. Understanding unsubscribeOn()
    9. Summary
  15. Switching, Throttling, Windowing, and Buffering
    1. Buffering
      1. Fixed-size buffering
      2. Time-based buffering
      3. Boundary-based buffering
    2. Windowing
      1. Fixed-size windowing
      2. Time-based windowing
      3. Boundary-based windowing
    3. Throttling
      1. throttleLast() or sample()
      2. throttleFirst()
      3. throttleWithTimeout() or debounce()
    4. Switching
    5. Grouping keystrokes
    6. Summary
  16. Flowable and Backpressure
    1. Understanding backpressure
      1. An example that needs backpressure
      2. Introducing the Flowable
      3. When to use Flowable and backpressure
    2. Understanding Flowable and Subscriber
      1. Interface Subscriber
    3. Creating Flowable
      1. Using Flowable.create() and BackpressureStrategy
      2. Turning Observable into Flowable and vice versa
    4. Using onBackpressureXXX() operators
      1. onBackPressureBuffer()
      2. onBackPressureLatest()
      3. onBackPressureDrop()
    5. Using Flowable.generate()
    6. Summary
  17. Transformers and Custom Operators
    1. Composing new operators from existing ones using compose() and transformers
      1. Using ObservableTransformer
      2. Using FlowableTransformer
      3. Avoiding a shared state with transformers
    2. Using the to() operator for fluent conversion
    3. Creating new operators from scratch using lift()
      1. Implementing an ObservableOperator
      2. Implementing a FlowableOperator
    4. Creating a new operator for Single, Maybe, or Completable
    5. Using RxJava2-Extras and RxJava2Extensions
    6. Summary
  18. Section 3: Integration of RxJava applications
  19. Testing and Debugging
    1. Configuring JUnit
    2. Blocking subscribers
    3. Blocking operators
      1. blockingFirst()
      2. blockingGet()
      3. blockingLast()
      4. blockingIterable()
      5. blockingForEach()
      6. blockingNext()
      7. blockingLatest()
      8. blockingMostRecent()
    4. Using TestObserver and TestSubscriber
    5. Manipulating time with TestScheduler
    6. Debugging RxJava code
    7. Summary
  20. RxJava on Android
    1. Creating an Android project
      1. Configuring Retrolambda
      2. Configuring RxJava and friends
    2. Using RxJava and RxAndroid
      1. Using RxBinding
      2. Other RxAndroid bindings libraries
      3. Life cycles and cautions using RxJava with Android
    3. Summary
  21. Using RxJava for Kotlin
    1. Why Kotlin?
    2. Configuring Kotlin
      1. Configuring Kotlin with Gradle
      2. Configuring Kotlin with Maven
      3. Configuring RxJava and RxKotlin
    3. Kotlin basics
      1. Creating a Kotlin file
      2. Assigning properties and variables
      3. Extension functions
      4. Kotlin lambda expressions
    4. Extension operators
    5. Using RxKotlin
    6. Dealing with SAM ambiguity
    7. Using let() and apply()
      1. Using let()
      2. Using apply()
    8. Tuple and data classes
    9. The future of ReactiveX and Kotlin
    10. Summary
  22. Appendix A: Introducing Lambda Expressions
    1. Introducing lambda expressions
      1. Implementing Runnable using lambda expression
      2. Making a Supplier a lambda
      3. Making a Consumer a lambda
      4. Making a function a lambda
  23. Appendix B: Functional Types
    1. Functional types
  24. Appendix C: Mixing Object-Oriented and Reactive Programming
    1. Mixing object-oriented and reactive programming
  25. Appendix D: Materializing and Dematerializing
    1. Materializing and dematerializing
  26. Appendix E: Understanding Schedulers
    1. Understanding Schedulers
  27. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learning RxJava - Second Edition
  • Author(s): Nick Samoylov, Thomas Nield
  • Release date: February 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789950151