presentation - we live in a reactive world - techforumiberia2016

29
09-02-2016 © For internal use © For internal use We live in a reactive world Introduction to reactive programming based on Android

Upload: sergi-castillo-malpesa

Post on 15-Apr-2017

98 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Presentation - We live in a reactive world - TechForumIberia2016

09-02-2016

© For internal use© For internal use

We live in a reactive world

Introduction to reactive programming based on Android

Page 2: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 3: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 4: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 5: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 6: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 7: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 8: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 9: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 10: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 11: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 12: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 13: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ The actors– Observable– Subscriber– Subscription

13

Page 14: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ The actors– Observable– Subscriber– Subscription

14

Page 15: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ The actors– Observable– Subscriber– Subscription

15

Page 16: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ The actors– Observable– Subscriber– Subscription

16

Page 17: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 18: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ Observable creation

18

Page 19: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 20: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ Composition

20

Page 21: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ Composition

21

Page 22: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 23: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ Transformation

23

Page 24: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 25: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

RxJava

▶ Multi-threading

25

Page 26: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 27: Presentation - We live in a reactive world - TechForumIberia2016

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

Page 28: Presentation - We live in a reactive world - TechForumIberia2016

9th February 2016 | TechForum eXplore Iberia 2016 | © For internal use

We live in a reactive world

28

Page 29: Presentation - We live in a reactive world - TechForumIberia2016

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]