2. ABOUT ME
Bart Kuipers
Freelance Mobile Developer
Xamarin & Flutter.
• CEO @ Baku-Apps
• Senior Mobile Developer @ Minibrew
• Restaurant ordering startup
• DevOps project @ Xablu
3. AGENDA
1. Why use Reactive Extensions (Rx)?
2. What are Reactive Extensions
3. Demo Time!
4. Advantages & Disadvantages of Rx
5. Question Time!
4. WHY USE RX?
User Input
(taps, typing)
Social
Media
Messaging
Internet of
Things (IoT)
GPS
Real-time
Stock Prices
5. WHAT ARE REACTIVE EXTENSIONS (RX)
Event Streams
Observabl
e
Observer
LINQ to Events
• Select
• Where
• First
• FirstOrDefault
• Last
• LastOrDefault
• GroupBy
• Sum
• Skip
• Take
• Count
• Single
• TakeUntill
• Buffer
• DistinctUntilChange
d
• Sampel
• Delay
• Retry
• Timeout
• Many more…..
12. SUPER DUPER AWESOME FUN WITH
FLAGS DEMO APP!
1. Delay search request after each keystroke for 0.5 seconds.
2. Handle delayed search requests. Discard previous search
request and only show latest results.
3. Only search request after search term > 1 characters.
4. Only perform search request until search term is changed
5. Have a 30 seconds timeout for the server response.
14. ADVANTAGES DISADVATAGES
• More expressive and readable code
• Handle streams of data, and pass that
handle around
• A lot of operators that simplify work
• Complex threading becomes easy
• Basically same workings and
operators in different language
(RxJava, RxJS, RxSwift, RxKotlin, etc)
• Steep learning curve
• Can be difficult to debug
• Small performance hit
This talk is a very gentle introduction to Reactive Extensions (RX.NET). And to give you an overview of Reactive extensions and why you should care.
Because the world today is reactive.
Why should we care -> Data is being received by event streams and less by actively asking for data. Data is coming at you and you have to react to that data.
Examples are: stock tickers, messengers and social media and GPS, and other sensors on the phone (step counter).
History:
Father of RX is Eric Meijer (a Dutch computer scientist/researcher working for Microsoft)
Rx manifesto
Push & Pull models (IEnumerable vs Observable & IEnumerator vs Observer)
RX released in 2009
Talk about Observer pattern. We have an Observable which sends a stream of data and we have an observer which subscribes to the Observable
This can all be concurrent asynchronous code!!!
Show Marbles (Select/map & Throtteling & DistinctUntil)
Observer patterns: Producer/Sender/Observable -----> Consumer/Listener/Observer (we will be using Observables and Observers)
Task are great but:
not lazy -> they run right away you declare them.
Not restartable -> if you await a task again you get the exact same value
Writing asynchronous code with Tasks can become very complex as we will see later in the quick demo.
Why RX
RX are the opposite -> they are Lazy (only fire when listened to/observed) and they are restartable. If you listen to a RX again -> it will give you fresh data (maybe the same but fresh).
RX allows as small and readable code (more expressive as they would say)
Testable -> same piece of code run concurrently in production and synchronously in unit test
Also RX is an almost uniform language you can use for many different languages… and will have almost similar operations and how it works
Start using events can cause problems.
Should be unsubscribed (main source of memory leaks).
Islands or events methods, difficult to reason about the flow of the program.
If I want to change some logic before the event (e.g. filter) we have to change the event method. We cannot “easily” edit it.
Events are not “first-class citizens”… meaning I cannot just take the event and send it to an argument of another method.
Timeout when not response in 30 sec
Observable are a push model, meaning we don’t have all the values currently. This introduces another dimension, the time dimension. There are many time operators, and as we saw in the demo pretty easy to use.
Rx is compatible with asynchronous programming tasks, async/await.
It is possible to very easily convert a Task to an Observable using the ToObservable extension method or use Observable.FromAsync().
We can await an Observable. However it will only return the last value of the stream. So in the demo of the case I explained for Akavache it will get only the last send notification/value before the stream is completed.