SlideShare a Scribd company logo
1 of 39
Putting the logic where it should be
Rx Architectures
in Android
#LiveCode
@tehmou
@futurice
@BeMyAppGermany
@co_up
This is not a
product on sale 
But a new way of seeing software
05/20/14
Classical Scenario:
loading a web page
» 1. User writes the url in the address bar
» 2. Browser sends a request and shows a loading bar
» 3. ???
» 4. Profit
Imperative Way
A = 0
B = A + 1
A = 2
- Place the value of 0 into variable A
- Read the value of A, calculate A + 1 and place it into B
- Place the value 2 into A
Result: B remains untouched
Modern Scenario:
Android App
» User navigates within the app
» Friends list is refreshed in the background
» Message delivery fails (for a message sent 20 seconds ago in
another screen)
» Application is suddenly suspended
» FarmVille request arrives and is shown as an in-app notification
Result:
» Any part of any view may be updated at any time
» The app is no longer a simple command line script but a complex
system of multiple input and entry points
So we need just more callbacks.. or?
7Futurice
RxJava
805/20/14 Futurice
Observable
05/20/14
// Implemented elsewhere
Observable<String> observable;
Observer<String> observer = new Observer<String>() {
public void onCompleted() { }
public void onError(Throwable e) { }
public void onNext(String s) {
// Handle new value
}
};
observable.subscribe(observer);
Observer pattern
05/20/14
observable.onNext(“Hello World!”);
observable.onComplete();
This sends a new value to every observer that is subscribed.
onNext is in FRP terms the same as setting the value of a
variable.
So it’s just a
pub/sub?
That’s it?
05/20/14
Observable Observer
Well, kind of.
Network
Observable
Disk store
parse cache
get additional
information
Network again
View logic
UI
Network
Observable
Disk store
parse cache
get additional
information
Network again
View logic
UIIt’s a typed
data flow
string Book POJO
Author POJO
View Model
property
… of pure logic
Network
Observable
Disk store
parse cache
get additional
information
Network again
View logic
UI
.. the entry points
of which we know
exactly.
Content Provider
Network
Adding new
features
Network
Observable
Disk store
parse cache
get additional
information
Network again
View logic
UI
Plugging non-rx things into the system
1705/20/14Futurice
FutuVille API
Websocket
Observable wrapper
observable
emitted
onNext
Adding new
features
Network
Observable
Disk store
parse cache
get additional
information
Network again
View logic
UI
UI state
handler
FutuVille
Values/events can go
into the system only
from the designated
entry points.
that’s a period
Variable vs. Observable
• Observable fills the role of a variable in FRP systems
• The subscriber (observer) is expected to react consistently every
time a new value is set
• The new value is used to do whatever changes necessary (UI state,
storage) and it is then discarded
• Saving the incoming values is usually not necessary and it can be
dangerous – holding state leads to bugs
History of Rx
» Developed by Microsoft as Reactive Extensions
» “...is a library to compose asynchronous and event-based programs
using observable collections and LINQ-style query operators.”
» Has been ported on most platforms and languages (except PHP)
2105/20/14 Futurice
Rx is..
» Also known as Functional Reactive Programming
» A way of composing (pure) functions into processing chains
» A way to avoid callback hell
» A very fancy event publish / listen mechanism
Characteristics of Rx Apps
» Basic building blocks of a program become observables – not
variables
» Favoring push over pull (reacting to change instead of polling it)
» Observables do not hold state that can be pulled, but they instead
emit values whenever something changes
» You can declare “pipes” or “flows” within the app that have defined
entry points for data process it in a deterministic way
What is Rx good for?
» Applications nowadays are increasingly asynchronous and imperative
programming is not enough for app logic
» Data can come into the application from many points
» … or to not come
» Reactive chains make sure the correct action happens each time new data
arrives
» The entry points for unexpected data are clearly defined
» Processing of asynchronous streams, such as throttling and composing
Goals of system design
» Pass immutable objects through the system
» Combine elaborate data reliably
» Keep all data dependencies up-to-date
» Permanent subscriptions as event buses
» View models for increased testability
So in which parts of the app
should we use RxJava?
26Futurice
All of them!
27Futurice
Complete Rx Skeleton
Network layer
Content
Provider Websocket
Data
processing
DataLayer
Fragment
ViewModel
View
UI State
Handler
create and destroy
manage events / data
that require showing
dialogs etc.
One-off
subscriptions,
essentially async calls
Data Layer
» Gives the last cached value to new subscribers
» Uses the network client to fetch data
» Offers open subscriptions for receiving a new value whenever a
data entry changes (usually identified by an Uri string)
» Keeps track of all needy observers
» Can refresh all data in the background
View Models
» Contains all logic necessary for processing “backend data” into
rendereable values
» Upon subscription the latest value is immediate emitted and after
that refreshed as necessary
» Bindings (subscriptions) are done with weak references
» Subscribe to the appropriate data sources in Data Store
» Unsubscribes from everything whenever the owner is destroyed
Views
» Plain layouts and drawing code
» No further processing of values received from view models
The Problems in Android
» RxJava subscriptions create strong references – memory leaks if
not unsubscribed
» Unsubscribing at the right time is hard especially with nested view
models
» No established view model / binding structure
» Rx in general forces one to think more and closes many shortcuts
» Bridging the gap between native components and view models can
sometimes be challenging since the views hold complex state
Why You Should do It?
» Cleaner code
» Makes you understand where bugs come from
» It feels right
Resources
» Github sample project: https://github.com/tehmou/rx-android-
architecture
» My blog post: http://blog.futurice.com/top-7-tips-for-rxjava-on-android
» Official RxJavaWiki: https://github.com/Netflix/RxJava/wiki
» A good intro to programming RxJava on
Android:http://mttkay.github.io/blog/2013/08/25/functional-reactive-
programming-on-android-with-rxjava/
» Original Rx: http://msdn.microsoft.com/en-gb/data/gg577609.aspx
TimoTuominen
Software Consultant
@tehmou
timo.tuominen@gmail.com
+49 176 10340729
Contact
Futurice
Futurice in
brief
HELSINKI
TAMPERE
BERLIN
LONDON
200
Founded in
2000
1
2013: 20M€
1
We believe that our values – trust, caring, transparency and
continuous improvement – are the key to our happiness cycle
that includes happy customers, happy people and happy end-
users!
We have two GPTW Institute’s Best Workplace victories in a row
on European level. Futurice is the first company to ever achieve
this. This enables us to recruit the best talent in the market.
Futurice is the best place to work in
whole Europe
» Small, efficient teams of passionate and
dedicated people
» Modern ways of doing, modern
technologies
» Short lead times, Lean mindset
» Design and technology under the same
roof, zero hand-overs!
» Consumer grade user experience also for
How we do things
P.S. We’re hiring... :)

More Related Content

Viewers also liked

RxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtRxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtConstantine Mars
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidFernando Cejas
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015Constantine Mars
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaIntro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaMike Nakhimovich
 
Reark : a Reference Architecture for Android using RxJava
Reark : a Reference Architecture for Android using RxJavaReark : a Reference Architecture for Android using RxJava
Reark : a Reference Architecture for Android using RxJavaFuturice
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomáš Kypta
 
Reactive programming with Rxjava
Reactive programming with RxjavaReactive programming with Rxjava
Reactive programming with RxjavaChristophe Marchal
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaKros Huang
 
Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in AndroidAli Muzaffar
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Mario Fusco
 
Retrofit Android by Chris Ollenburg
Retrofit Android by Chris OllenburgRetrofit Android by Chris Ollenburg
Retrofit Android by Chris OllenburgTrey Robinson
 
RxJava - introduction & design
RxJava - introduction & designRxJava - introduction & design
RxJava - introduction & designallegro.tech
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 

Viewers also liked (19)

RxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtRxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArt
 
Rx java in action
Rx java in actionRx java in action
Rx java in action
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
 
Android antipatterns
Android antipatternsAndroid antipatterns
Android antipatterns
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
RxBinding-kotlin
RxBinding-kotlinRxBinding-kotlin
RxBinding-kotlin
 
Intro to Functional Programming with RxJava
Intro to Functional Programming with RxJavaIntro to Functional Programming with RxJava
Intro to Functional Programming with RxJava
 
Reark : a Reference Architecture for Android using RxJava
Reark : a Reference Architecture for Android using RxJavaReark : a Reference Architecture for Android using RxJava
Reark : a Reference Architecture for Android using RxJava
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
Reactive programming with Rxjava
Reactive programming with RxjavaReactive programming with Rxjava
Reactive programming with Rxjava
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
Kotlinにお触り
Kotlinにお触りKotlinにお触り
Kotlinにお触り
 
Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in Android
 
Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...Reactive Programming for a demanding world: building event-driven and respons...
Reactive Programming for a demanding world: building event-driven and respons...
 
Retrofit Android by Chris Ollenburg
Retrofit Android by Chris OllenburgRetrofit Android by Chris Ollenburg
Retrofit Android by Chris Ollenburg
 
RxJava - introduction & design
RxJava - introduction & designRxJava - introduction & design
RxJava - introduction & design
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 

Recently uploaded

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 

Recently uploaded (20)

VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 

RxJava Architectures on Android - Android LiveCode Berlin

  • 1. Putting the logic where it should be Rx Architectures in Android #LiveCode @tehmou @futurice @BeMyAppGermany @co_up
  • 2. This is not a product on sale  But a new way of seeing software 05/20/14
  • 3. Classical Scenario: loading a web page » 1. User writes the url in the address bar » 2. Browser sends a request and shows a loading bar » 3. ??? » 4. Profit
  • 4. Imperative Way A = 0 B = A + 1 A = 2 - Place the value of 0 into variable A - Read the value of A, calculate A + 1 and place it into B - Place the value 2 into A Result: B remains untouched
  • 5. Modern Scenario: Android App » User navigates within the app » Friends list is refreshed in the background » Message delivery fails (for a message sent 20 seconds ago in another screen) » Application is suddenly suspended » FarmVille request arrives and is shown as an in-app notification
  • 6. Result: » Any part of any view may be updated at any time » The app is no longer a simple command line script but a complex system of multiple input and entry points
  • 7. So we need just more callbacks.. or? 7Futurice
  • 9. Observable 05/20/14 // Implemented elsewhere Observable<String> observable; Observer<String> observer = new Observer<String>() { public void onCompleted() { } public void onError(Throwable e) { } public void onNext(String s) { // Handle new value } }; observable.subscribe(observer);
  • 10. Observer pattern 05/20/14 observable.onNext(“Hello World!”); observable.onComplete(); This sends a new value to every observer that is subscribed. onNext is in FRP terms the same as setting the value of a variable.
  • 11. So it’s just a pub/sub? That’s it? 05/20/14 Observable Observer
  • 12. Well, kind of. Network Observable Disk store parse cache get additional information Network again View logic UI
  • 13. Network Observable Disk store parse cache get additional information Network again View logic UIIt’s a typed data flow string Book POJO Author POJO View Model property … of pure logic
  • 14. Network Observable Disk store parse cache get additional information Network again View logic UI .. the entry points of which we know exactly. Content Provider Network
  • 15. Adding new features Network Observable Disk store parse cache get additional information Network again View logic UI
  • 16. Plugging non-rx things into the system 1705/20/14Futurice FutuVille API Websocket Observable wrapper observable emitted onNext
  • 17. Adding new features Network Observable Disk store parse cache get additional information Network again View logic UI UI state handler FutuVille
  • 18. Values/events can go into the system only from the designated entry points. that’s a period
  • 19. Variable vs. Observable • Observable fills the role of a variable in FRP systems • The subscriber (observer) is expected to react consistently every time a new value is set • The new value is used to do whatever changes necessary (UI state, storage) and it is then discarded • Saving the incoming values is usually not necessary and it can be dangerous – holding state leads to bugs
  • 20. History of Rx » Developed by Microsoft as Reactive Extensions » “...is a library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators.” » Has been ported on most platforms and languages (except PHP) 2105/20/14 Futurice
  • 21. Rx is.. » Also known as Functional Reactive Programming » A way of composing (pure) functions into processing chains » A way to avoid callback hell » A very fancy event publish / listen mechanism
  • 22. Characteristics of Rx Apps » Basic building blocks of a program become observables – not variables » Favoring push over pull (reacting to change instead of polling it) » Observables do not hold state that can be pulled, but they instead emit values whenever something changes » You can declare “pipes” or “flows” within the app that have defined entry points for data process it in a deterministic way
  • 23. What is Rx good for? » Applications nowadays are increasingly asynchronous and imperative programming is not enough for app logic » Data can come into the application from many points » … or to not come » Reactive chains make sure the correct action happens each time new data arrives » The entry points for unexpected data are clearly defined » Processing of asynchronous streams, such as throttling and composing
  • 24. Goals of system design » Pass immutable objects through the system » Combine elaborate data reliably » Keep all data dependencies up-to-date » Permanent subscriptions as event buses » View models for increased testability
  • 25. So in which parts of the app should we use RxJava? 26Futurice
  • 27. Complete Rx Skeleton Network layer Content Provider Websocket Data processing DataLayer Fragment ViewModel View UI State Handler create and destroy manage events / data that require showing dialogs etc. One-off subscriptions, essentially async calls
  • 28. Data Layer » Gives the last cached value to new subscribers » Uses the network client to fetch data » Offers open subscriptions for receiving a new value whenever a data entry changes (usually identified by an Uri string) » Keeps track of all needy observers » Can refresh all data in the background
  • 29. View Models » Contains all logic necessary for processing “backend data” into rendereable values » Upon subscription the latest value is immediate emitted and after that refreshed as necessary » Bindings (subscriptions) are done with weak references » Subscribe to the appropriate data sources in Data Store » Unsubscribes from everything whenever the owner is destroyed
  • 30. Views » Plain layouts and drawing code » No further processing of values received from view models
  • 31. The Problems in Android » RxJava subscriptions create strong references – memory leaks if not unsubscribed » Unsubscribing at the right time is hard especially with nested view models » No established view model / binding structure » Rx in general forces one to think more and closes many shortcuts » Bridging the gap between native components and view models can sometimes be challenging since the views hold complex state
  • 32. Why You Should do It? » Cleaner code » Makes you understand where bugs come from » It feels right
  • 33. Resources » Github sample project: https://github.com/tehmou/rx-android- architecture » My blog post: http://blog.futurice.com/top-7-tips-for-rxjava-on-android » Official RxJavaWiki: https://github.com/Netflix/RxJava/wiki » A good intro to programming RxJava on Android:http://mttkay.github.io/blog/2013/08/25/functional-reactive- programming-on-android-with-rxjava/ » Original Rx: http://msdn.microsoft.com/en-gb/data/gg577609.aspx
  • 37. We believe that our values – trust, caring, transparency and continuous improvement – are the key to our happiness cycle that includes happy customers, happy people and happy end- users! We have two GPTW Institute’s Best Workplace victories in a row on European level. Futurice is the first company to ever achieve this. This enables us to recruit the best talent in the market. Futurice is the best place to work in whole Europe
  • 38. » Small, efficient teams of passionate and dedicated people » Modern ways of doing, modern technologies » Short lead times, Lean mindset » Design and technology under the same roof, zero hand-overs! » Consumer grade user experience also for How we do things