Slide 1

Slide 1 text

Common mobile architecture

Slide 2

Slide 2 text

Your hosts Mike Seghers (@mikeseghers) Filip Maelbrancke (@fmaelbrancke) Consultants at iDA MediaFoundry (@iDAMediaFoundry)

Slide 3

Slide 3 text

Why this talk?

Slide 4

Slide 4 text

First off... Why mobile architecture? What is it? How to implement?

Slide 5

Slide 5 text

Architecture

Slide 6

Slide 6 text

You = McGuyver

Slide 7

Slide 7 text

Avoid problems

Slide 8

Slide 8 text

Build up confidence

Slide 9

Slide 9 text

! Nerdvana “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” Tony Hoare

Slide 10

Slide 10 text

Relation to enterprise architecture?

Slide 11

Slide 11 text

Layered architecture

Slide 12

Slide 12 text

Dependency Injection

Slide 13

Slide 13 text

Relation to rich client architecture?

Slide 14

Slide 14 text

MVC View Model Controller Data retrieval Data changes User Gestures View Selection Events

Slide 15

Slide 15 text

Mobile challenges

Slide 16

Slide 16 text

Remoting

Slide 17

Slide 17 text

Data handling

Slide 18

Slide 18 text

Remoting

Slide 19

Slide 19 text

Naive client GUI thread View Remoting logic Transformation logic Storage logic Presentation logic CRUD operations Save data structure Remote Local storage

Slide 20

Slide 20 text

The problems with the naive approach

Slide 21

Slide 21 text

Naive approach = Let your users wait

Slide 22

Slide 22 text

A less naive approach GUI thread View Presentation logic Background thread Remote Local storage CRUD operations Save data structure Service Remoting logic Transformation logic Storage logic

Slide 23

Slide 23 text

Working towards a layered architecture Communication with remote systems Caching data in local storage Translation of data Keeping state of the cached data Passing data to the view layer

Slide 24

Slide 24 text

Remoting Services Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers

Slide 25

Slide 25 text

Adaptors Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers Adaptor Entities

Slide 26

Slide 26 text

Storage Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers Adaptor Data Source Entities

Slide 27

Slide 27 text

Local Storage Service Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers Adaptor Data Source Entities Local Service

Slide 28

Slide 28 text

Data Synchronisation What is still missing? Something to tie the services together and communicate with the GUI layer. Something to keep track of the state of our local data. Eg. when do we need to refresh the data from the backend?

Slide 29

Slide 29 text

Dataprovider Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers Adaptor Data Source Entities Local Service Data Provider GUI Layer

Slide 30

Slide 30 text

Statekeeper Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers Adaptor Data Source Entities Local Service Data Provider State Keeper GUI Layer

Slide 31

Slide 31 text

Non-blocking? We now know that we will handle data using the data provider, which encapsulates all data handling logic. However, we still didn’t tackle the “my-main- thread-is-blocked-for-a-while” problem.

Slide 32

Slide 32 text

Making things asynchronous 1. Using Events/Notifications

Slide 33

Slide 33 text

Making things asynchronous: Events Pros: Loose coupling Easy to implement Cons: Hard to maintain Hard to follow where all events end up Event storms!

Slide 34

Slide 34 text

Making things asynchronous 2. Callback system

Slide 35

Slide 35 text

Making things asynchronous: Callbacks Pros: Loose coupling Easy to refactor Easy to follow and to maintain Cons: Little more complex then eventing

Slide 36

Slide 36 text

Making things asynchronous: Implementing a callback system Responders and Callhandles

Slide 37

Slide 37 text

Responder explained caller callee calls • on X • on Y • on Z immediately returns • on X • on Y • on Z • on X • on Y • on Z other actors • on X • on Y • on Z

Slide 38

Slide 38 text

Callhandles explained callee caller calls immediately returns • on X • on Y • on Z

Slide 39

Slide 39 text

Tying it up! Remoting Service Remote API HTTP/JSON/XML AMF ProtocolBuffers Adaptor Data Source Entities Local Service Data Provider State Keeper GUI Layer Responder/ Callhandle Responder/ Callhandle

Slide 40

Slide 40 text

Back-end communication technologies

Slide 41

Slide 41 text

Back-end communication? SOAP RMI REST

Slide 42

Slide 42 text

REST

Slide 43

Slide 43 text

Backend optimizations?

Slide 44

Slide 44 text

Compress!

Slide 45

Slide 45 text

DRY

Slide 46

Slide 46 text

Data handling

Slide 47

Slide 47 text

CRUD via REST Method Operation Change? Idempotent? GET GET / SELECT ✔ ✔ POST INSERT ✘ ✘ PUT UPDATE ✘ ✔ DELETE DELETE ✘ ✔

Slide 48

Slide 48 text

POST Data Provider + State Keeper Remote Service Data Source insert set STATE_INSERTING update clear STATE_INSERTING

Slide 49

Slide 49 text

PUT update set STATE_UPDATING update clear STATE_UPDATING Data Provider + State Keeper Remote Service Data Source

Slide 50

Slide 50 text

DELETE update set STATE_DELETING delete Data Provider + State Keeper Remote Service Data Source

Slide 51

Slide 51 text

GET insert new resources Data Provider + State Keeper Remote Service Data Source

Slide 52

Slide 52 text

Data Provider 1 Responder Callhandle 1 No local data Front layer Local Service 2 State Watcher 3 Remote Service 4 Responder Callhandle 4 5 State Keeper 6 7

Slide 53

Slide 53 text

Data up-to-date Front layer Data Provider 1 Responder Callhandle 1 Local Service 2 State Watcher 3 State Keeper 4

Slide 54

Slide 54 text

Data outdated Front layer Data Provider 1 Responder Callhandle 1 Local Service 2 State Watcher 3 4 8 Up-to- date data Remote Service 5 Responder Callhandle 5 6 State Keeper 7 Stale data

Slide 55

Slide 55 text

Conclusions Do not call backend logic inside UI threads Start long running operations from a background service Persist early and persist often Minimize the network usage

Slide 56

Slide 56 text

References

Slide 57

Slide 57 text

Suggested reading Design patterns: elements of reusable object-oriented software Gamma et al. (978-0201633610) Patterns of enterprise application architecture Fowler et al. (978-0321127426) The pragmatic programmer Hunt, Thomas (978-0201616224)

Slide 58

Slide 58 text

Suggested viewing WWDC sessions Google IO sessions

Slide 59

Slide 59 text

Q + A

Slide 60

Slide 60 text

Contact information iDA MediaFoundry [email protected] @iDAMediaFoundry Filip [email protected] @fmaelbrancke Mike [email protected] @mikeseghers