Top Banner
The Hard but Right Path Rx Architectures in Android
27

RxJava Architectures on Android #8 Android LiveCode

May 10, 2015

Download

Technology

Timo Tuominen

The slides for London Android LiveCode talk on Mar 20th. You can find the sample project at https://github.com/tehmou/rx-android-architecture
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RxJava Architectures on Android #8 Android LiveCode

The Hard but Right Path

Rx Architecturesin Android

Page 2: RxJava Architectures on Android #8 Android LiveCode

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. Web page loads

Page 3: RxJava Architectures on Android #8 Android LiveCode

Imperative WayA = 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

Page 4: RxJava Architectures on Android #8 Android LiveCode

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)

» FarmVille request arrives and is shown as an in-app notification

» Application is suddenly suspended

Page 5: RxJava Architectures on Android #8 Android LiveCode

Declarative WayA = 0

B = A + 1

A = 2

- Emit the value of 0 from observable A

- Define B to be A + 1

- Emit the value 2 from observable A

Result: Everyone interested in B was also updated with the value of 3

Page 6: RxJava Architectures on Android #8 Android LiveCode

How to do declarativeon Android then?

6Futurice

Page 7: RxJava Architectures on Android #8 Android LiveCode

RxJava

704/11/23

Futurice

Page 8: RxJava Architectures on Android #8 Android LiveCode

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)

804/11/23

Futurice

Page 9: RxJava Architectures on Android #8 Android LiveCode

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

Page 10: RxJava Architectures on Android #8 Android LiveCode

The Observable» Observables are objects that emit values

» The emitted values are immutable, a new object comes when something changes

» You can subscribe to an observable to receive the emitted values henceforth

» Operations can be performed on the values that observables emit (a kind of a wrapper pattern)

» Multiple observables can be combined into one with different kinds of conditions, resulting in one observable

Page 11: RxJava Architectures on Android #8 Android LiveCode

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

Page 12: RxJava Architectures on Android #8 Android LiveCode

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

Page 13: RxJava Architectures on Android #8 Android LiveCode

Our Goals» 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

Page 14: RxJava Architectures on Android #8 Android LiveCode

So in which parts of the app

should we use RxJava?

14Futurice

Page 15: RxJava Architectures on Android #8 Android LiveCode

All of them!

15Futurice

Page 16: RxJava Architectures on Android #8 Android LiveCode

Complete Rx StackNetwork Client

Data Store

View Models

Views

Activity or Fragmentcreate and

destroy

stateless / one-off

preserved state (cache)

weak referencesubscription

subscribe

Page 17: RxJava Architectures on Android #8 Android LiveCode

Data Store» Uses the network client to fetch data

» Offers open subscriptions for receiving a new value whenever a data entry changes (usually identified by an ID string)

» Can refresh all data in the background

» If you have a book, it can automatically fetch the author from a different API

Page 18: RxJava Architectures on Android #8 Android LiveCode

View Models» Contains all logic necessary for processing “backend

data” into rendereable values

» Expose refined subscribable properties

» 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

Page 19: RxJava Architectures on Android #8 Android LiveCode

Views» Plain layouts and drawing code

» No further processing of values received from view models

Page 20: RxJava Architectures on Android #8 Android LiveCode

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 structure (we wrote our own)

» 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

Page 21: RxJava Architectures on Android #8 Android LiveCode

Why You Should do It?» Cleaner code

» Makes you understand where bugs come from

» It feels right

Page 22: RxJava Architectures on Android #8 Android LiveCode

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 RxJava Wiki: 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

Page 23: RxJava Architectures on Android #8 Android LiveCode

Timo TuominenIndependent Software

Consultant@tehmou

[email protected]+44 7459 371157

Contact

Page 24: RxJava Architectures on Android #8 Android LiveCode

Futurice

Page 25: RxJava Architectures on Android #8 Android LiveCode

Futurice in brief

HELSINKI

TAMPERE

BERLINLONDON

200

Founded in2000

1

2013: 20M€

1

Page 26: RxJava Architectures on Android #8 Android LiveCode

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

Page 27: RxJava Architectures on Android #8 Android LiveCode

» 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 Enterprise IT

How we do things