rxjava architectures on android - android livecode berlin
DESCRIPTION
Presentation slides from 19th of May 2014. Functional Reactive Programming is about moving away from traditional state management with variables and using streams of data instead. The streams can be composed and distilled in any way imaginable, allowing for a truly dynamic system. However, despite the inherent beauty of the approach, it is not a bed of roses but requires an update to the entire way of seeing software as a system.TRANSCRIPT
![Page 1: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/1.jpg)
Putting the logic where it should be
Rx Architecturesin Android
#LiveCode@tehmou@futurice
@BeMyAppGermany@co_up
![Page 2: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/2.jpg)
This is not a product on sale
But a new way of seeing software04/07/23
![Page 3: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/3.jpg)
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
![Page 4: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/4.jpg)
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 5: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/5.jpg)
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
![Page 6: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/6.jpg)
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
![Page 7: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/7.jpg)
So we need just more callbacks.. or?
7Futurice
![Page 8: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/8.jpg)
RxJava
804/07/23
Futurice
![Page 9: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/9.jpg)
Observable
04/07/23
// Implemented elsewhereObservable<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);
![Page 10: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/10.jpg)
Observer pattern
04/07/23
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.
![Page 11: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/11.jpg)
So it’s just a pub/sub?That’s it?
04/07/23
Observable Observer
![Page 12: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/12.jpg)
Well, kind of.
NetworkObservable
Disk store
parse cache
get additional information
Network again
View logic
UI
![Page 13: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/13.jpg)
NetworkObservable
Disk store
parse cache
get additional information
Network again
View logic
UIIt’s a typeddata flowstring Book POJO
Author POJO
View Modelproperty
… of pure logic
![Page 14: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/14.jpg)
NetworkObservable
Disk store
parse cache
get additional information
Network again
View logic
UI.. the entry pointsof which we knowexactly.
Content Provider
Network
![Page 15: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/15.jpg)
Adding newfeatures
NetworkObservable
Disk store
parse cache
get additional information
Network again
View logic
UI
![Page 16: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/16.jpg)
Plugging non-rx things into the system
1704/07/23Futurice
FutuVille APIWebsocket
Observable wrapper
observable emittedonNext
![Page 17: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/17.jpg)
Adding newfeatures
NetworkObservable
Disk store
parse cache
get additional information
Network again
View logic
UI
UI state handler
FutuVille
![Page 18: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/18.jpg)
Values/events can go into the system only from the designated entry points.that’s a period
![Page 19: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/19.jpg)
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
![Page 20: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/20.jpg)
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)
2104/07/23
Futurice
![Page 21: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/21.jpg)
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 22: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/22.jpg)
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 23: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/23.jpg)
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 24: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/24.jpg)
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
![Page 25: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/25.jpg)
So in which parts of the appshould we use RxJava?
26Futurice
![Page 26: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/26.jpg)
All of them!
27Futurice
![Page 27: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/27.jpg)
Complete Rx Skeleton
Network layer
ContentProvider Websocket
Data processin
g
DataLayer
Fragment
ViewModel
View
UI State Handler
create and destroy
manage events / datathat require showingdialogs etc.
One-off subscriptions,essentially async calls
![Page 28: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/28.jpg)
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
![Page 29: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/29.jpg)
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
![Page 30: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/30.jpg)
Views» Plain layouts and drawing code» No further processing of values received from view
models
![Page 31: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/31.jpg)
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
![Page 32: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/32.jpg)
Why You Should do It?» Cleaner code» Makes you understand where bugs come from» It feels right
![Page 33: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/33.jpg)
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 35: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/35.jpg)
Futurice
![Page 36: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/36.jpg)
Futurice in brief
HELSINKITAMPERE
BERLINLONDON
200
Founded in2000
1
2013: 20M€
1
![Page 37: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/37.jpg)
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 38: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/38.jpg)
» 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
![Page 39: RxJava Architectures on Android - Android LiveCode Berlin](https://reader033.vdocuments.net/reader033/viewer/2022050804/53fddb888d7f72a81c8b4b54/html5/thumbnails/39.jpg)