presentation - we live in a reactive world - techforumiberia2016
TRANSCRIPT
09-02-2016
© For internal use© For internal use
We live in a reactive world
Introduction to reactive programming based on Android
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
About the author
▶ Sergi Castillo Malpesa– Android Deputy Team Lead at Worldline– 9 years of experience in Java– More than 3 years developing mobile apps– Architecture enthusiast and clean code evangelist
▶ Meet me!– @SmasSive– https://plus.google.com/u/0/+SergiCastillo– https://github.com/SmasSive
2
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Index
▶ What is RxJava▶ Motivation▶ What we have– Callbacks– Event bus
▶ RxJava– Observer pattern– The actors– Observable creation– Composition– Transformation– Multi-threading– Error handling
▶ Conclusions
3
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Index
▶ What is RxJava▶ Motivation▶ What we have– Callbacks– Event bus
▶ RxJava– Observer pattern– The actors– Observable creation– Composition– Transformation– Multi-threading– Error handling
▶ Conclusions
4
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
What is RxJava
▶ Java VM implementation of Reactive eXtensions
▶ Not a new concept: Rx come from .NET
▶ Ported to Java by Netflix
▶ A library for composing asynchronous and event-based programs by using observable sequences
▶ Streams… Streams!!!
5
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Index
▶ What is RxJava▶ Motivation▶ What we have– Callbacks– Event bus
▶ RxJava– Observer pattern– The actors– Observable creation– Composition– Transformation– Multi-threading– Error handling
▶ Conclusions
6
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Motivation
▶ We live in a reactive world
▶ Humans don’t really enjoy waiting
▶ Mobile: Real-time data & push
▶ React to user inputs: events!
▶ Focus on the business logic
7
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Index
▶ What is RxJava▶ Motivation▶ What we have– Callbacks– Event bus
▶ RxJava– Observer pattern– The actors– Observable creation– Composition– Transformation– Multi-threading– Error handling
▶ Conclusions
8
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
What we have: Callbacks
▶ Callback pattern can be messy
▶ Different implementations for the same result
▶ Boilerplate code
9
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
What we have: Event bus
▶ Subscribe and produce events
▶ No connection between producer and listener
▶ Composition of several events
▶ Concurrency
▶ Error handling
10
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Index
▶ What is RxJava▶ Motivation▶ What we have– Callbacks– Event bus
▶ RxJava– Observer pattern– The actors– Observable creation– Transformation– Composition– Multi-threading– Error handling
▶ Conclusions
11
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ How it works?– Extends the observer pattern– Adds support to sequences of data (streams!)– Adds transformation tools of those streams– Adds the ability to compose from several streams– Adds multi-threading
12
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ The actors– Observable– Subscriber– Subscription
13
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ The actors– Observable– Subscriber– Subscription
14
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ The actors– Observable– Subscriber– Subscription
15
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ The actors– Observable– Subscriber– Subscription
16
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Observable creation– create()– just()– from()– repeat()– empty()– never()– interval()– timer()– And much more…
17
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Observable creation
18
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Composition– Rx gives us a set of tools to compose streams– We can combine different sources of data into just one– Useful methods:• merge()• concat()• zip()• …
19
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Composition
20
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Composition
21
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Transformation– Rx gives us a set of tools to transform the data emitted by
streams– Useful methods:• map()• flatMap()• concatMap()• buffer()• …
22
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Transformation
23
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Multi-threading– Schedulers– RxJava offers five kind of Schedulers• io()• computation()• inmediate()• newThread()• trampoline()
– AndroidSchedulers.mainThread()– SubscribeOn– ObserveOn
24
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
RxJava
▶ Multi-threading
25
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Index
▶ What is RxJava▶ Motivation▶ What we have– Callbacks– Event bus
▶ RxJava– Observer pattern– The actors– Observable creation– Transformation– Composition– Multi-threading– Error handling
▶ Conclusions
26
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
Conclusions
▶ Pros– Event-based development– Clean code– A lot of tools to manipulate the data– Easy multi-threading
▶ Cons– Learning curve– Verbosity– Debugging
27
9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use
We live in a reactive world
28
Worldline is a registered trademark of Atos Worldline SAS. June 2013© 2013 Atos. Confidential information owned by Atos Worldline, to be used by the recipient only. This document, or any part of it, may not be reproduced, copied, circulated and/or distributed nor quoted without prior written approval from Atos Worldline.
09-02-2016
© For internal use
Worldline is a registered trademark of Atos Worldline SAS. June 2013© 2013 Atos. Confidential information owned by Atos Worldline, to be used by the recipient only. This document, or any part of it, may not be reproduced, copied, circulated and/or distributed nor quoted without prior written approval from Atos Worldline.
© For internal use
ThanksFor more information please contact:[email protected]