Top Banner
reark.io Powered by Futurice Spice Program
125

Reark : a Reference Architecture for Android using RxJava

Feb 10, 2017

Download

Software

Futurice
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: Reark : a Reference Architecture for Android using RxJava

reark.ioPowered by Futurice Spice Program

Page 2: Reark : a Reference Architecture for Android using RxJava

reark.ioPowered by Futurice Spice Program

Page 3: Reark : a Reference Architecture for Android using RxJava

reark.ioPowered by Futurice Spice Program

That’s 1952 stars

Page 4: Reark : a Reference Architecture for Android using RxJava

reark.io started as an example of how we do

things.

Page 5: Reark : a Reference Architecture for Android using RxJava

Now it has library

components as well.

Page 6: Reark : a Reference Architecture for Android using RxJava

Why native, and not

React Native?

Page 7: Reark : a Reference Architecture for Android using RxJava

Who am I?

Page 8: Reark : a Reference Architecture for Android using RxJava

@tehmou• Reactive

programmer

Page 9: Reark : a Reference Architecture for Android using RxJava

History of reark.io

Page 10: Reark : a Reference Architecture for Android using RxJava

SAMSUNG KICKPowered by Futurice

Page 11: Reark : a Reference Architecture for Android using RxJava
Page 12: Reark : a Reference Architecture for Android using RxJava

Samsung KICK had an average score of 4.1 out of 5 on Google Play store, it was downloaded over 3 million times.

Page 13: Reark : a Reference Architecture for Android using RxJava

It was rebranded into Goal+ summer 2016.

Page 14: Reark : a Reference Architecture for Android using RxJava

Statistics

Page 15: Reark : a Reference Architecture for Android using RxJava

Total Lines of Code155384

(546410 added, 391026 removed)

Total Commits10103

Authors31

Page 16: Reark : a Reference Architecture for Android using RxJava

Commit HistoryMWC Demo Tablet release

(1089 commits in May)

Ramp up

Phone release

Page 17: Reark : a Reference Architecture for Android using RxJava

A lot of challenges keeping the code together.

Page 18: Reark : a Reference Architecture for Android using RxJava

Android 4.1.x has a bug that crashes it for view hierarchies deeper than ~30.

On a complex tablet application with nested fragments under 30 is a dream world.

Page 19: Reark : a Reference Architecture for Android using RxJava

- Big codebase- Large velocity- Long- “Satellite” apps

Page 20: Reark : a Reference Architecture for Android using RxJava

What does the app look

like?

Page 21: Reark : a Reference Architecture for Android using RxJava
Page 22: Reark : a Reference Architecture for Android using RxJava

Main App

First there was an app.

Page 23: Reark : a Reference Architecture for Android using RxJava

Main App

Store

View

“Database” to hold the data and publish updates.

What is shown on the screen by the platform.

Page 24: Reark : a Reference Architecture for Android using RxJava

Main App

Store

View Model

View

Push updates to the data.

Process the raw data.

Display data.

Page 25: Reark : a Reference Architecture for Android using RxJava

This is the MVVM architecture, you can see an example in the reark.io sample.

Page 26: Reark : a Reference Architecture for Android using RxJava

Main App

Page 27: Reark : a Reference Architecture for Android using RxJava

Main App

Process

A process has its own “stack”, which means that other processes

do not see variables in it.

Page 28: Reark : a Reference Architecture for Android using RxJava

Main App

Process

Product owner wants to make a cool new widget. It has to run in

its own “remote process”.

Process

Page 29: Reark : a Reference Architecture for Android using RxJava

Main App

KICK Livewidget

ProcessProcess

Now we have two processes that both have their own memory

space.

Page 30: Reark : a Reference Architecture for Android using RxJava

Main App

KICK Livewidget

ProcessProcess

StoreBut the Store is stuck in

one process.

Page 31: Reark : a Reference Architecture for Android using RxJava

Main App

KICK Livewidget

ProcessProcess

StoreBut the Store is stuck in

one process.

Page 32: Reark : a Reference Architecture for Android using RxJava

Main App

KICK Livewidget

ProcessProcess

Store Move store into a shared container.

Page 33: Reark : a Reference Architecture for Android using RxJava

Main App

KICK Livewidget

ProcessProcess

Provide data and changes to

all attached processes.

Store

Page 34: Reark : a Reference Architecture for Android using RxJava

StoreWhat about

other devices connected to the phone?

Page 35: Reark : a Reference Architecture for Android using RxJava

Store

Gear

SmartTV

Page 36: Reark : a Reference Architecture for Android using RxJava

StoreUse Samsung web socket bridge to publish updates.

Page 37: Reark : a Reference Architecture for Android using RxJava

One Store for different uses:• Inside of one process• Between processes• Between physical devices

Page 38: Reark : a Reference Architecture for Android using RxJava

What’s in the Store then?

Page 39: Reark : a Reference Architecture for Android using RxJava

On Android the Store is traditionally an database.SQLite

Also known as ContentProvider.

Page 40: Reark : a Reference Architecture for Android using RxJava

SQLite- Can be shared across processes - Allows to observe changes - Asynchronous read / write

Page 41: Reark : a Reference Architecture for Android using RxJava

SQLite

- It’s been there since the beginning

- Can be shared across processes - Allows to observe changes - Asynchronous read / write

Page 42: Reark : a Reference Architecture for Android using RxJava

What’s the problem?

Page 43: Reark : a Reference Architecture for Android using RxJava

SQLite

- … - A pain to implement!

Page 44: Reark : a Reference Architecture for Android using RxJava

SQLite

- Nobody got time for that!

Page 45: Reark : a Reference Architecture for Android using RxJava

What if there was a way to start with a simple Store that

could be later changed into an SQLite?

Page 46: Reark : a Reference Architecture for Android using RxJava

reark.ioPowered by

Spice Program

Page 47: Reark : a Reference Architecture for Android using RxJava

reark.io[x] Scalable Android Architecture

Powered by Spice Program

Page 48: Reark : a Reference Architecture for Android using RxJava

Introduce an MVP Store that can use SQLite internally.

Page 49: Reark : a Reference Architecture for Android using RxJava

… or not use is it until it is needed.

Page 50: Reark : a Reference Architecture for Android using RxJava

Generic App v0.1Super simple proof of concept.

Flickr: infinity7664

Page 51: Reark : a Reference Architecture for Android using RxJava

Generic App v0.01Super simple proof of concept.

Flickr: infinity7664

PO

Page 52: Reark : a Reference Architecture for Android using RxJava

Generic App v0.01Super simple proof of concept.

Flickr: infinity7664

This is just a demo, it will

never go into production!

PO

Page 53: Reark : a Reference Architecture for Android using RxJava

Generic App v0.01Super simple proof of concept.

Flickr: infinity7664

This is just a demo, it will

never go into production!

PO

Page 54: Reark : a Reference Architecture for Android using RxJava

Generic App v0.01Super simple proof of concept.

Flickr: infinity7664

This is just a demo, it will

never go into production!

PO

Page 55: Reark : a Reference Architecture for Android using RxJava

Generic App v0.01Super simple proof of concept.

Flickr: infinity7664

This is just a demo, it will

never go into production!

PO

Page 56: Reark : a Reference Architecture for Android using RxJava

Wrote an app that was not going into

production Bad Luck Brian

Page 57: Reark : a Reference Architecture for Android using RxJava

Wrote an app that was not going into

production

It went into production

Page 58: Reark : a Reference Architecture for Android using RxJava

The version 0.01Super simple proof of concept version. Everything in one process.

Store

Page 59: Reark : a Reference Architecture for Android using RxJava

The version 0.01Super simple proof of concept version. Everything in one process.

Store

new HashMap<Integer, Beer>

Page 60: Reark : a Reference Architecture for Android using RxJava

The version 0.01Store becomes a wrapper for the HashMap. It is a simple key-value store.

Storenew HashMap<Integer, Beer>

Page 61: Reark : a Reference Architecture for Android using RxJava

The version 0.01

Storenew HashMap<Integer, Beer>

Great! But how to get beer out of the store?

Page 62: Reark : a Reference Architecture for Android using RxJava

The version 0.01

Storenew HashMap<Integer, Beer>Beer getBeer(Integer beerId)

Page 63: Reark : a Reference Architecture for Android using RxJava

The version 0.01

Storenew HashMap<Integer, Beer>Beer getBeer(Integer beerId)

= Ask for beer, get beer immediately

Page 64: Reark : a Reference Architecture for Android using RxJava

The version 0.01

Storenew HashMap<Integer, Beer>Beer getBeer(Integer beerId)

Not sure if KISS

Or the worst idea ever

Page 65: Reark : a Reference Architecture for Android using RxJava

SQLite- Can be shared across processes - Allows to track changes - Asynchronous read / write

Page 66: Reark : a Reference Architecture for Android using RxJava

Bad Luck Brian

Wrote his app with KISS

Page 67: Reark : a Reference Architecture for Android using RxJava

Wrote his app with KISS

It didn’t scale

Page 68: Reark : a Reference Architecture for Android using RxJava
Page 69: Reark : a Reference Architecture for Android using RxJava

What’s the minimum interface for a good Store?

Page 70: Reark : a Reference Architecture for Android using RxJava

Store interface

StorePut an item at a time

Get a stream of

items

Page 71: Reark : a Reference Architecture for Android using RxJava

Store interface

StorePut an item at a time

Get a stream of

items(with the same id)

Page 72: Reark : a Reference Architecture for Android using RxJava

RxJava and Observables

Page 73: Reark : a Reference Architecture for Android using RxJava

Observables in very short

store.getBeer(beerId)We define a way to get beer from the

Store.

Page 74: Reark : a Reference Architecture for Android using RxJava

Observables in very short

store.getBeer(beerId)

For this we need to know the beerId

Page 75: Reark : a Reference Architecture for Android using RxJava

Observables in very short

store.getBeer(beerId) .subscribe(

We will subscribe to the Observable. This

is like a callback.

Page 76: Reark : a Reference Architecture for Android using RxJava

Observables in very short

store.getBeer(beerId) .subscribe( beer -> drink(beer) );

We define the action that is executed every

time a new beer arrives.

Page 77: Reark : a Reference Architecture for Android using RxJava

Observables in very short

store.getBeer(beerId) .subscribe( beer -> drink(beer) );

Notice that we will always get updated versions of the beer

with the same id.

Page 78: Reark : a Reference Architecture for Android using RxJava

How it works in real life.

Page 79: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer StoreLet’s create a beer Store.

Page 80: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer Store

First customer!

Page 81: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer Store

Another subscriber to the same beer!

Page 82: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer StoreSomeone pushes an update to the Store.

Page 83: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer StoreThe update is

propagated to all subscribers.

Page 84: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer Store

Page 85: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer StoreThe subscribers have processed the latest

update.

Page 86: Reark : a Reference Architecture for Android using RxJava

What about new subscriber that missed the beer?

Page 87: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer Store

A new subscriber arrives!

Page 88: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer StoreThe new subscriber gets the latest value immediately to

get up to date.

Page 89: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer Store

Page 90: Reark : a Reference Architecture for Android using RxJava

Store interface

Beer StoreAll subscribers are again on

stand-by for new values.

Page 91: Reark : a Reference Architecture for Android using RxJava

The reark.io Store design.

Page 92: Reark : a Reference Architecture for Android using RxJava

Store interfaceinterface StoreInterface <T, U> { void put(T item); Observable<T> get(U id); }

Page 93: Reark : a Reference Architecture for Android using RxJava

Store interfaceinterface StoreInterface <T, U> { void put(T item); Observable<T> get(U id); }

The put operation is fire and forger.

Page 94: Reark : a Reference Architecture for Android using RxJava

Store interfaceinterface StoreInterface <T, U> { void put(T item); Observable<T> get(U id); }

The is no synchronous get, only an observable.

Page 95: Reark : a Reference Architecture for Android using RxJava

The two main reark.io Store types.

Page 96: Reark : a Reference Architecture for Android using RxJava

MemoryStore

Flickr: infinity7664

Page 97: Reark : a Reference Architecture for Android using RxJava

MemoryStoreimplements StoreInterface <T, U>

Flickr: infinity7664

Page 98: Reark : a Reference Architecture for Android using RxJava

MemoryStoreimplements StoreInterface <T, U>

Flickr: infinity7664

Good for:• Getting started• No need to persist data• Only one process running

Page 99: Reark : a Reference Architecture for Android using RxJava

ContentProviderStoreimplements StoreInterface <T, U>

Flickr: Toti Maragliano

Page 100: Reark : a Reference Architecture for Android using RxJava

ContentProviderStoreimplements StoreInterface <T, U>

The interface is the same

Flickr: Toti Maragliano

Page 101: Reark : a Reference Architecture for Android using RxJava

ContentProviderStoreimplements StoreInterface <T, U>

Flickr: Toti Maragliano

Good for:• Persisting data if app is shut down• Sharing data between processes, i.e. widgets• If you have a bit of time to set up the SQLite

Page 102: Reark : a Reference Architecture for Android using RxJava

Case study: Quickbeer Next

Powered by Spice Program

https://github.com/apoi/quickbeer-next

Page 103: Reark : a Reference Architecture for Android using RxJava

App for ratebeer.com API

Wikipedia: “RateBeer has over 4.5 million ratings of almost 200,000 beers, from nearly 16,000 breweries”

Page 104: Reark : a Reference Architecture for Android using RxJava

Hand crafted by: apoi

https://github.com/apoi/quickbeer-next

Powered by Spice Program

Page 105: Reark : a Reference Architecture for Android using RxJava

Hand crafted by: apoi

https://github.com/apoi/quickbeer-next

Powered by Spice ProgramI have over 1100 beer

ratings on my ratebeer.com account that I use to test the app. You could say it’s well-brewed.

Page 106: Reark : a Reference Architecture for Android using RxJava
Page 107: Reark : a Reference Architecture for Android using RxJava

Main Contributors

Page 108: Reark : a Reference Architecture for Android using RxJava

apoi

Main Contributors

Develops the framework against his beer app as well as (secret) customer projects.

Page 109: Reark : a Reference Architecture for Android using RxJava

apoi recuutus

Main Contributors

The Dagger master, also does excellent reviews and tests.

Page 110: Reark : a Reference Architecture for Android using RxJava

apoi tomaszpolanskirecuutus

Main ContributorsFiguring out a way to put his Options library in the example project.

Page 111: Reark : a Reference Architecture for Android using RxJava

apoi tomaszpolanskirecuutus

Main ContributorsFiguring out a way to put his Options library in the example project.

Page 112: Reark : a Reference Architecture for Android using RxJava

apoi tomaszpolanskirecuutus

Main ContributorsFiguring out a way to put his Options library in the example project.

Page 113: Reark : a Reference Architecture for Android using RxJava

apoi tomaszpolanskirecuutus

Main ContributorsFiguring out a way to put his Options library in the example project.

Page 114: Reark : a Reference Architecture for Android using RxJava

prt2121apoi tomaszpolanskirecuutus

Main Contributors

We don’t really know who he is, but he sometimes updates libraries.

Page 115: Reark : a Reference Architecture for Android using RxJava

prt2121apoi tomaszpolanskirecuutus

Main Contributors

Started it, now drops in every once in a while and annoys everyone by rewriting stuff.

tehmou

Page 116: Reark : a Reference Architecture for Android using RxJava

Thank you.

Page 117: Reark : a Reference Architecture for Android using RxJava

Thank you.(star us on reark.io)

@tehmou Timo Tuominen

Page 118: Reark : a Reference Architecture for Android using RxJava

Extra case study: Nautics Sailmate

Page 119: Reark : a Reference Architecture for Android using RxJava
Page 120: Reark : a Reference Architecture for Android using RxJava

“More convenient sailing”

Page 121: Reark : a Reference Architecture for Android using RxJava

This is a sea chart.

Page 122: Reark : a Reference Architecture for Android using RxJava

An offline package is downloaded with hundreds of Points

of Interest.

Page 123: Reark : a Reference Architecture for Android using RxJava

The POIs are written all at once

into an SQLite Store.

Store

Page 124: Reark : a Reference Architecture for Android using RxJava

Every part of the app is updated instantly when a

download finishes.

Store

Page 125: Reark : a Reference Architecture for Android using RxJava

fin.