Book Description
RxJava is a library for composing asynchronous and event-based programs using Observable sequences for the JVM, allowing developers to build robust applications in less time.
Learning RxJava addresses all the fundamentals of reactive programming to help readers write reactive code, as well as teach them an effective approach to designing and implementing reactive libraries and applications.
Starting with a brief introduction to reactive programming concepts, there is an overview of Observables and Observers, the core components of RxJava, and how to combine different streams of data and events together. You will also learn simpler ways to achieve concurrency and remain highly performant, with no need for synchronization. Later on, we will leverage backpressure and other strategies to cope with rapidly-producing sources to prevent bottlenecks in your application. After covering custom operators, testing, and debugging, the book dives into hands-on examples using RxJava on Android as well as Kotlin.
What You Will Learn
Learn the features of RxJava 2 that bring about many significant changes, including new reactive types such as Flowable, Single, Maybe, and Completable
Understand how reactive programming works and the mindset to "think reactively"
Demystify the Observable and how it quickly expresses data and events as sequences
Learn the various Rx operators that transform, filter, and combine data and event sequences
Leverage multicasting to push data to multiple destinations, and cache and replay them
Discover how concurrency and parallelization work in RxJava, and how it makes these traditionally complex tasks trivial to implement
Apply RxJava and Retrolambda to the Android domain to create responsive Android apps with better user experiences
Use RxJava with the Kotlin language to express RxJava more idiomatically with extension functions, data classes, and other Kotlin features
Author(s): Thomas Nield
Series: true pdf (not conversion)
Publisher: Packt
Year: 2017
Language: English
Commentary: True pdf (not conversion).
Pages: 393
Cover......Page 0
Credits......Page 4
About the Author......Page 5
Acknowledgements......Page 6
About the Reviewers......Page 8
www.PacktPub.com......Page 9
Customer Feedback......Page 10
Table of Contents......Page 11
Preface......Page 18
Chapter 1: Thinking Reactively......Page 24
A brief history of ReactiveX and RxJava......Page 25
Thinking reactively......Page 26
Why should I learn RxJava?......Page 27
Setting up......Page 28
Navigating the Central Repository......Page 29
Using Gradle......Page 30
Using Maven......Page 32
A quick exposure to RxJava......Page 33
When to use RxJava......Page 37
Summary......Page 38
How Observables work......Page 40
Using Observable.create()......Page 41
Using Observable.just()......Page 45
The Observer interface......Page 47
Implementing and subscribing to an Observer......Page 48
Shorthand Observers with lambdas......Page 49
Cold versus hot Observables......Page 51
Cold Observables......Page 52
Hot Observables......Page 55
ConnectableObservable......Page 57
Observable.range()......Page 59
Observable.interval()......Page 61
Observable.future()......Page 64
Observable.never()......Page 65
Observable.error()......Page 66
Observable.defer()......Page 67
Observable.fromCallable()......Page 70
Single......Page 71
Maybe......Page 72
Completable......Page 74
Disposing......Page 75
Handling a Disposable within an Observer......Page 76
Using CompositeDisposable......Page 78
Handling Disposal with Observable.create()......Page 79
Summary......Page 81
Suppressing operators......Page 82
take()......Page 83
skip()......Page 85
takeWhile() and skipWhile()......Page 86
distinct()......Page 87
distinctUntilChanged()......Page 89
elementAt()......Page 90
map()......Page 91
startWith()......Page 92
switchIfEmpty()......Page 94
sorted()......Page 95
delay()......Page 97
repeat()......Page 98
scan()......Page 99
count()......Page 101
reduce()......Page 102
all()......Page 103
contains()......Page 104
Collection operators......Page 105
toList()......Page 106
toMap() and toMultiMap()......Page 107
collect()......Page 110
Error recovery operators......Page 111
onErrorReturn() and onErrorReturnItem()......Page 112
onErrorResumeNext()......Page 114
retry()......Page 116
doOnNext(), doOnComplete(), and doOnError()......Page 118
doOnSubscribe() and doOnDispose()......Page 120
Summary......Page 122
Chapter 4: Combining Observables......Page 124
Observable.merge() and mergeWith()......Page 125
flatMap()......Page 129
Concatenation......Page 134
Observable.concat() and concatWith()......Page 135
concatMap()......Page 137
Ambiguous......Page 138
Zipping......Page 140
Combine latest......Page 142
withLatestFrom()......Page 144
Grouping......Page 145
Summary......Page 147
Chapter 5: Multicasting, Replaying, and Caching......Page 149
Understanding multicasting......Page 150
Multicasting with operators......Page 151
When to multicast......Page 156
Automatic connection......Page 158
autoConnect()......Page 159
refCount() and share()......Page 162
Replaying......Page 164
Caching......Page 169
PublishSubject......Page 170
When to use Subjects......Page 171
When Subjects go wrong......Page 173
Serializing Subjects......Page 174
BehaviorSubject......Page 175
ReplaySubject......Page 176
AsyncSubject......Page 177
UnicastSubject......Page 178
Summary......Page 181
Why concurrency is necessary......Page 182
Concurrency in a nutshell......Page 183
Introducing RxJava concurrency......Page 184
Keeping an application alive......Page 190
Understanding Schedulers......Page 193
New thread......Page 194
Trampoline......Page 195
ExecutorService......Page 196
Understanding subscribeOn()......Page 197
Nuances of subscribeOn()......Page 201
Understanding observeOn()......Page 204
Using observeOn() for UI event threads......Page 208
Nuances of observeOn()......Page 210
Parallelization......Page 211
unsubscribeOn()......Page 216
Summary......Page 219
Chapter 7: Switching, Throttling, Windowing, and Buffering......Page 220
Fixed-size buffering......Page 221
Time-based buffering......Page 224
Boundary-based buffering......Page 226
Fixed-size windowing......Page 227
Time-based windowing......Page 229
Boundary-based windowing......Page 230
Throttling......Page 231
throttleLast() / sample()......Page 233
throttleWithTimeout() / debounce()......Page 234
Switching......Page 236
Grouping keystrokes......Page 241
Summary......Page 244
Understanding backpressure......Page 245
An example that needs backpressure......Page 247
Introducing the Flowable......Page 249
Use an Observable If.........Page 251
Use a Flowable If.........Page 252
Understanding the Flowable and Subscriber......Page 253
The Subscriber......Page 254
Creating a Flowable......Page 259
Using Flowable.create() and BackpressureStrategy......Page 260
Turning an Observable into a Flowable (and vice-versa)......Page 262
onBackPressureBuffer()......Page 264
onBackPressureLatest()......Page 267
onBackPressureDrop()......Page 268
Using Flowable.generate()......Page 269
Summary......Page 273
Transformers......Page 274
ObservableTransformer......Page 275
FlowableTransformer......Page 279
Avoiding shared state with Transformers......Page 280
Using to() for fluent conversion......Page 283
Implementing an ObservableOperator......Page 286
FlowableOperator......Page 291
Custom Transformers and operators for Singles, Maybes, and Completables......Page 294
Using RxJava2-Extras and RxJava2Extensions......Page 295
Summary......Page 296
Chapter 10: Testing and Debugging......Page 298
Blocking subscribers......Page 299
Blocking operators......Page 302
blockingFirst()......Page 303
blockingGet()......Page 304
blockingLast()......Page 305
blockingIterable()......Page 306
blockingNext()......Page 307
blockingLatest()......Page 308
blockingMostRecent()......Page 309
Using TestObserver and TestSubscriber......Page 310
Manipulating time with the TestScheduler......Page 312
Debugging RxJava code......Page 314
Summary......Page 319
Chapter 11: RxJava on Android......Page 320
Creating the Android project......Page 321
Configuring Retrolambda......Page 327
Configuring RxJava and friends......Page 330
Using RxJava and RxAndroid......Page 331
Using RxBinding......Page 335
Other RxAndroid bindings libraries......Page 338
Life cycles and cautions using RxJava with Android......Page 339
Summary......Page 343
Chapter 12: Using RxJava for Kotlin New......Page 344
Configuring Kotlin......Page 345
Configuring Kotlin for Maven......Page 346
Kotlin basics......Page 348
Creating a Kotlin file......Page 349
Assigning properties and variables......Page 350
Extension functions......Page 351
Kotlin lambdas......Page 352
Extension operators......Page 354
Using RxKotlin......Page 356
Dealing with SAM ambiguity......Page 357
Using let()......Page 359
Using apply()......Page 361
Tuples and data classes......Page 362
Future of ReactiveX and Kotlin......Page 364
Summary......Page 365
Making a Runnable a lambda......Page 366
Making a Supplier a lambda......Page 368
Making a Consumer a lambda......Page 370
Making a Function a lambda......Page 372
Functional types......Page 374
Mixing object-oriented and reactive programming......Page 375
Materializing and Dematerializing......Page 380
Understanding Schedulers......Page 383
Index......Page 387