SlideShare ist ein Scribd-Unternehmen logo
1 von 86
Downloaden Sie, um offline zu lesen
ASYNCHRONOUS
TECHNIQUES FOR ANDROID:
PROGRAMMING SIDEWAYS
Emanuele	
  Di	
  Saverio	
  
AND02
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Senior	
  Design	
  Technologist	
  
emanuele.disaverio@gmail.com	
  
hazam	
  
emanueledisaverio	
  
Who?
"Agilist	
  in	
  a	
  Mobile	
  world"	
  
•  Mobile	
  Java	
  Developer	
  (2007)	
  
•  Android	
  (2009)	
  
•  h@p://www.androidavanzato.it	
  (2012)	
  
Emanuele	
  Di	
  Saverio	
  
Senior	
  Design	
  Technologist	
  @	
  
TWO RECURRING
PROBLEMS, WITH
SIMILAR SYMPTOMS
1ST PROBLEM
A (biased) TALE ABOUT
SERVERS
The S in C/S
•  a	
  server	
  program	
  in	
  (connected)	
  network	
  compuKng	
  
while (true) {	
	connect();	
	read();	
	parseRequest();	
	process();	
	sendResponse();	
	disconnect();	
}
MPM Prefork (Apache)
•  let's	
   fork() and	
  spawn	
  more	
  processes!	
  
full	
  address	
  space	
  copied	
  around	
  
MPM Worker (Apache)
•  handful	
  processes	
  spawn	
  a	
  set	
  of	
  threads	
  
context	
  switch	
  +	
  shared	
  memory	
  
Multi Threaded Programming
•  Memory	
  is	
  shared,	
  threads	
  spawn	
  faster	
  
•  Problem	
  of	
  synchronizaKon	
  –	
  solvable	
  only	
  
with	
  low-­‐level	
  OS	
  primiKves	
  
Multi Threaded Programming
•  Memory	
  is	
  shared,	
  threads	
  spawn	
  faster	
  
•  Problem	
  of	
  synchronizaKon	
  –	
  solvable	
  only	
  
with	
  low-­‐level	
  OS	
  primiKves	
  
"…	
  non-­‐trivial	
  mul/-­‐threaded	
  programs	
  are	
  incomprehensible	
  to	
  
humans.	
  It	
  is	
  true	
  that	
  the	
  programming	
  model	
  can	
  be	
  
improved:	
  design	
  pa;erns,	
  atomicity,	
  improved	
  languages,	
  and	
  
formal	
  methods.	
  However,	
  these	
  techniques	
  merely	
  chip	
  away	
  at	
  
the	
  unnecessarily	
  enormous	
  non-­‐determinism	
  of	
  the	
  threading	
  
model.	
  The	
  model	
  remains	
  intrinsically	
  intractable."	
  
Edward	
  A.	
  Lee	
  –	
  The	
  Problem	
  with	
  Threads	
  
CONTEXT SWITCH
has	
  fixed	
  cost.	
  
Context Switch
Context Switch
hint:	
  human	
  mind	
  work	
  same	
  way	
  
Single Thread Event
Old	
  technique	
  –	
  New	
  Trend	
  
Nginx / NodeJS / Jetty
•  User	
  Code	
  is	
  executed	
  by	
  single	
  
thread	
  
•  I/O	
  is	
  on	
  separate	
  thread	
  
MOBILE UI
ARCHITECTURE
a.k.a.	
  looked	
  from	
  a	
  high	
  enough	
  level,	
  they're	
  all	
  the	
  same.	
  	
  
The Loop
while (true) {	
	processInputEvents();	
	processSysEvents();	
	measure();	
	layout();	
	draw();	
}	
input	
  
sys	
  
measure	
  
layout	
  
draw	
  
thread	
  
Ok, when it is my turn?
HOLLYWOOD	
  PRINCIPLE	
  
"don't	
  call	
  me,	
  I'll	
  call	
  you"	
  
managed	
  execuKon	
  
LISTENERS	
  
Button a;	
[...]	
a.setOnClickListener(	
new View.OnClickListener() {	
	public void onClick(View v) {	
	//do stuff	
	}	
});	
object	
  that	
  receives	
  callback	
  
through	
  define	
  interface	
  
class MyActivity extends Activity {	
	
protected void onCreate(Bundle b) {	
	//create my UI	
}	
	
onStop() {...}	
	
onStart(){...}	
	
}
The Android Loop
All	
  callbacks	
  are	
  executed	
  in	
  Main	
  
Thread!	
  
•  AcKvity	
  
•  Service	
  
•  Fragment	
  
•  animaKons	
  
•  OnClicks	
  
•  OnTouch	
  
•  OnWhatever...	
  
	
  
	
  
android.os.Looper
Make	
  every	
  thread	
  an	
  event-­‐based	
  message	
  queue	
  
public void run() {	
	Looper.prepare();	
	this.handler = new Handler()	
	 	handleMessage(int what)	
	 	 	if (what == 0x666) //do stuff	
	};	
	Looper.loop();}	
handler.sendMessage(Message.obtain(0x666));
60 FPS -> 16 ms per frame
ALL YOUR CODE NEEDS TO FIT IN 16ms
The Loop
while (true) {	
	processInputEvents();	
	processSysEvents();	
	measure();	
	layout();	
	draw();	
}	
input	
  
sys	
  
measure	
  
layout	
  
draw	
  
thread	
  
16ms	
  
DON'T BLOCK UI THREAD
no	
  network	
  calls	
  -­‐	
  no	
  file	
  I/O	
  -­‐	
  no	
  intensive	
  computaKon	
  
DON'T BLOCK UI THREAD
no	
  network	
  calls	
  -­‐	
  no	
  file	
  I/O	
  -­‐	
  no	
  intensive	
  computaKon	
  
USE OTHER THREADS!
Asynchronous Java
Java was designed with multithreaded programming in
mind (thread ,synchronized, volatile, memory
model).
Don't rely on that – probabilities you really understand
what they do are small.
java.util.concurrent.*	
full of goodies to handle concurrency
Asynchronous Java
You don't really create Threads that
much
Executors – Thread pool implemented
for you
ExecutorService executor =
Executors.newFixedThreadPool(10);	
executor.execute(new Runnable() {	
	run() {	
	 	//do stuff	
	}	
});
Future – a computation Stock Option
Want	
  to	
  have	
  result	
  of	
  an	
  async	
  execuKon?	
  
	
	
	
	
	
	
	
	
Future<String> f1 = executor.submit(	
new Callable<String> {	
	String call() {	
	 	return fetchNetworkContent();	
	}	
});	
f1.get(); //locks current thread
Future – combining?
Future<String> f1 = executor.submit(...);	
Future<Integer> f2 = executor.submit(...);	
 	
Future<Integer> f3 = executor.submit(	
new [CALLABLE](f1.get()));	
Future<Integer> f4 = executor.submit(new
[CALLABLE]f2.get()));
Future – combining?
Future<String> f1 = executor.submit(...);	
Future<Integer> f2 = executor.submit(...);	
 	
Future<Integer> f3 = executor.submit(	
new [CALLABLE](f1.get()));	
Future<Integer> f4 = executor.submit(new
[CALLABLE]f2.get()));	
	
Future<String> f3 = executor.submit(new
Callable<String>() {	
call() {	
	return new CallToRemoteServiceC(f1.get()).call();	
}});
Future – Other Approaches
	
List<Future> futures = new ArrayList<Future>();	
	
Iterator<Future<?>> i = futures.iterator();	
	while (i.hasNext()) {	
	 	Future<?> f = i.next();	
	 	if (f.isDone()) {	
	 	 	doMoreWork(f.get());	
	i.remove();	
} }
GET() BLOCKS
COMBINATION IS A PAIN
Callback
Gets	
  around	
  the	
  early	
  blocking	
  problem	
  by	
  pre-­‐
declaring	
  the	
  funcKon	
  that	
  will	
  be	
  executed	
  
public interface Callback<T> {	
public void done(T result, 	
	 	 	 	 	 	 	Exception error);	
}
Callback
fetchNetworkContentAsync(Callback<Response> cb) {	
	mExecutor.execute(new Runnable(){	
	...	
	try {	
	 	Response r = fetchNetworkContent();	
	 	cb.done(r, null);	
	} catch(Exception exc) {	
	 	cb.done(null, exc);	
	});	
}
WAIT, WE NEED TO BE IN
MAIN THREAD
Callback
fetchNetworkContentAsync(Callback<Response> cb) {	
	mExecutor.execute(new Runnable(){	
	...	
	try {	
	 	Response r = fetchNetworkContent();	
	 	mHandler.post(new Runnable() { 		
	 	 	cb.done(r, null);	
	 	});	
	} catch(Exception exc) {	
	 	cb.done(null, exc);	
	});	
}
Callback
fetchNetworkContentAsync(Callback<Response> cb) {	
	mExecutor.execute(new Runnable(){	
	...	
	try {	
	 	Response r = fetchNetworkContent();	
	 	mExecutor.execute(new Runnable() {	
	 	 	try {	
	 	 	 	Response r2 = fetchNetworkContent2(r);	
	 	 	 	mHandler.post(new Runnable() { 		
	 	 	 	 	cb.done(r2, null);	
	 	 	 	});	
	} catch(Exception exc) {	
	 	cb.done(null, exc);	
	});	
	 	});	
	} catch(Exception exc) {	
	 	cb.done(null, exc);
Callback
fetchNetworkContentAsync(Callback<Response> cb) {	
	CountDownLatch cd = new CountDownLatch(2);	
	Response r1, r2;	
	mExecutor.execute(new Runnable(){	
	 	Response r1 = fetchNetworkContent();	
	 	cd.countDown();	
	});	
	mExecutor.execute(new Runnable(){	
	 	Response r2 = fetchNetworkContent();	
	 	cd.countDown();	
	});	
	mExecutor.execute(new Runnable(){ 	 	 	 	
	 	cd.await();	
	 	Response r = new Response(r1,r2);	
	 	mHandler.post(new Runnable() { 		
	 	 	 	 	cb.done(r, null);	
	 	});
ANDROID SDK HELP
Loader & AsyncTask
They	
  help	
  in	
  the	
  field	
  of	
  gedng	
  the	
  asynchronous	
  task	
  
happen	
  in	
  a	
  way	
  that	
  is	
  compaKble	
  with	
  Android	
  
threading	
  model,	
  but	
  fall	
  short	
  in:	
  
•  Combining	
  
•  ManipulaKng	
  
•  Error	
  Handling	
  
•  Streaming	
  Data	
  Handling	
  
AsyncTask
NEED MORE ASYNC
EXPRESSIVENESS
a.k.a.	
  my	
  head	
  is	
  hurKng	
  
ENTER
REACTIVE EXTENSIONS
Reactive Extensions (Rx)
Developed	
  by	
  Microsof	
  for	
  .NET	
  (C#)	
  2009	
  –	
  Erik	
  
Meijer	
  -­‐	
  to	
  support	
  distributed	
  cloud	
  applicaEons	
  
	
  
Open	
  Source	
  port	
  of	
  the	
  semanKcs	
  to	
  
•  Rx.ObjC,	
  RxJava,	
  Rx.JS,	
  Scala	
  
•  JavaVM	
  based	
  version	
  is	
  developed	
  by	
  Nejlix	
  –	
  
Ben	
  Christensen	
  
Rx (Java): Concepts
Observable	
  
EnKty	
  that	
  can	
  fetch	
  a	
  stream	
  of	
  events	
  
asyncronously,	
  if	
  you	
  subscribe	
  an	
  observer	
  to	
  it	
  
	
  
Observer	
  
You	
  can	
  imperaKvely	
  noKfy	
  an	
  observer	
  about	
  an	
  
event	
  	
  
	
  
Rx (Java): Sample
Observable<Employee> loadEmployeeWithName(String name) { 	
Observable<Employee> observable = Observable.create(	
	new Func1<Observer<Employee>, Subscription>() {	
	 	public Subscription call(final Observer<Employee> observer) {	
List<Employee> employee = loadEmployeeFromDB(name);	
	 	for (Employee emp : employees) {	
	 	 	try {	
	 	 	observer.onNext(emp);	
	 	 	 	} catch (Exception e) {	
	 	 	 	 	observer.onException(e);	
	 	 	 	}} 	
observer.onCompleted();	
return Subscriptions.empty();	
} });	
return observable;}	
loadEmployeeWithName("Aldo").subscribe(new Observer<Employee>() {	
	
	 	public void onNext(Employee emp) {	
	 	 	showEmployeeDataOnScreen(emp); 	}	
	 		
	 	public void onError(Exception e) {	
	 	 	handleException(e); }	
	
	 	public void onCompleted() {	
	 	 	doneLoadingEmployees();	
}});
"Async" expressed for collections
IMPERATIVE	
  
•  T	
  next()	
  
•  throws	
  ExcepKon	
  
•  return	
  
REACTIVE	
  
•  onNext(T)	
  
•  onError(ExcepKon)	
  
•  onCompleted()	
  
Rx (Java): Sample
Observable<Employee> loadEmployeeWithName(String name) { 	
Observable<Employee> observable = Observable.create(	
	new Func1<Observer<Employee>, Subscription>() {	
	 	public Subscription call(final Observer<Employee> observer) {	
List<Employee> employee = loadEmployeeFromDB(name);	
	 	for (Employee emp : employees) {	
	 	 	try {	
	 	 	observer.onNext(emp);	
	 	 	 	} catch (Exception e) {	
	 	 	 	 	observer.onException(e);	
	 	 	 	}} 	
observer.onCompleted();	
return Subscriptions.empty();	
} });	
return observable;}	
loadEmployeeWithName("Aldo").subscribe(new Observer<Employee>() {	
	
	 	public void onNext(Employee emp) {	
	 	 	showEmployeeDataOnScreen(emp); 	}	
	 		
	 	public void onError(Exception e) {	
	 	 	handleException(e); }	
	
	 	public void onCompleted() {	
	 	 	doneLoadingEmployees();	
}});	
	
STILL	
  SYNC	
  
Rx (Java): Sample
Observable<Employee> loadEmployeeWithName(String name) { 	
Observable<Employee> observable = Observable.create(	
	new Func1<Observer<Employee>, Subscription>() {	
	 	public Subscription call(final Observer<Employee> observer) {	
	 	 	executor.postRunnable(new Runnable() {...	
	
	List<Employee> employee = loadEmployeeFromDB(name);	
	 	 	for (Employee emp : employees) {	
	 	 	 	try {	
	 	 	 	 	 	mHandler.post(new Runnable() {	
	 	 	 	 	 	 	...	
	 	 	 	 	 	observer.onNext(emp);	
	 	 	 	 	 	 	 	...	
	 	 	 	 	 	}	
	 	 	 	 	} catch (Exception e) {	
	 	 	 	 	 	mHandler.post(new Runnable() {	
	 	 	 	 	 	 	...	
	 	 	 	 	 	 	 	observer.onException(e);	
	 	 	 	 	 	 	})	
	 	 	 	 	}} 	
	mHandler.post(new Runnable() {	
	 	 	 	 	observer.onCompleted();	
	 	 	 	});	
return Subscriptions.empty();	
} }); });	
return observable;}
So what is Rx really doing?
Rx	
  is	
  man-­‐in-­‐the	
  middle	
  between	
  method	
  
calling	
  and	
  callback	
  execuKon	
  
	
  
onNext() != onNext()	
	
We	
  have	
  control	
  over	
  scheduling	
  of	
  methods	
  
	
  
Rx (Java): Sample
Observable<Employee> loadEmployeeWithName(String name) { 	
Observable<Employee> observable = Observable.create(	
	new Func1<Observer<Employee>, Subscription>() {	
	 	public Subscription call(final Observer<Employee> observer)
{	
List<Employee> employee = loadEmployeeFromDB(name);	
	 	for (Employee emp : employees) {	
	 	 	try {	
	 	 	observer.onNext(emp);	
	 	 	 	} catch (Exception e) {	
	 	 	 	 	observer.onException(e);	
	 	 	 	}} 	
observer.onCompleted();	
return Subscriptions.empty();	
} });	
	
observable = observable.subscribeOn(Schedulers.executor(mExecutor));	
observable = observable.observeOn(AndroidScheduler.getInstance());	
return observable;	
}
Rx (Java): Sample
Observable<Employee> loadEmployeeWithName(String name) { 	
Observable<Employee> observable = Observable.create(	
	new Func1<Observer<Employee>, Subscription>() {	
	 	public Subscription call(final Observer<Employee> observer)
{	
List<Employee> employee = loadEmployeeFromDB(name);	
	 	for (Employee emp : employees) {	
	 	 	try {	
	 	 	observer.onNext(emp);	
	 	 	 	} catch (Exception e) {	
	 	 	 	 	observer.onException(e);	
	 	 	 	}} 	
observer.onCompleted();	
return Subscriptions.empty();	
} });	
	
observable = observable.subscribeOn(Schedulers.executor(mExecutor));	
observable = observable.observeOn(AndroidScheduler.getInstance());	
return observable;	
}	
loadEmployeeWithName("Aldo").subscribe(new Observer<Employee>() {	
	
	 	public void onNext(Employee emp) {	
	 	 	showEmployeeDataOnScreen(emp); 	}
Rx (Java): Features
Observer	
  is	
  executed	
  afer	
  someone	
  subscribed	
  	
  
Since	
  we	
  are	
  decoupling	
  method	
  call	
  and	
  callback	
  
execuKon,	
  we	
  can	
  easily	
  manipulate	
  and	
  combine.	
  
Chain Observables
Observable.concat(obs1,	
  obs2);	
  
Filter Observables
Observable.filter(obs1,	
  new	
  Func1<>()	
  {});	
  
Zip Observables
Observable.zip(obs1,	
  obs2,	
  new	
  Func1<a,b>()	
  {});	
  
Rx (Java): Features
Each	
  observer	
  can	
  be	
  scheduled	
  to	
  have	
  his	
  
subscribe	
  happen	
  on	
  any	
  thread,	
  and	
  all	
  
observaKon	
  happen	
  on	
  any	
  other	
  thread!	
  
Observable<Image> getImage(URL url) {	
	AtomicBoolean found = new AtomicBoolean(false);	
	obs1 = //get image from in memory cache	
	obs2 = //get image from disc cache	
	obs2 = obs2	
	.subscribeOn(Schedulers.executor(highprioexecutor))
	.observeOn(AndroidScheduler.getInstance());	
	obs3 = //get image from network	
	obs3 = obs3	
	.subscribeOn(Schedulers.executor(lowprioexecutor))
	.observeOn(AndroidScheduler.getInstance()); 		
	return Observable.concat(obs1, obs2, obs3);	
}
Rx (Java): Features
Connectable	
  version	
  of	
  Observable	
  can	
  be	
  
subscribed	
  mulKple	
  Kmes,	
  doesn't	
  restart	
  but	
  goes	
  
on,	
  can	
  even	
  replay	
  past	
  events!	
  
Observable<Tweet> obs = createObservableThatStreamTweet();	
void initObs() {	
	obs.replay();	
	obs = obs.connect(); 		
}	
	
Observable<Tweet> getPopularTweets() {	
	return obs;	
}
Reactive the world
•  We	
  could	
  wrap	
  all	
  applicaKon	
  in	
  reacKves:	
  	
  
click	
  events	
  could	
  generate	
  events	
  that	
  can	
  route	
  to	
  DB	
  calls.	
  View	
  could	
  
subscribe	
  to	
  DB	
  observable	
  and...	
  
	
  
Yes.	
  Maybe.	
  
Reactive Over Services
<<sync>>	
  
Employee	
  DAO	
  
<<sync>>	
  
Paypal	
  API	
  
<<async>>	
  
Android	
  NFC	
  Intents	
  
RX	
  Layer	
  
UI	
  /	
  Controllers	
  
FragmentA	
   FragmentA	
  AcKvity	
   Views	
  
2ND PROBLEM
Fragments
Fragments
NOT A UI but a LIFECYCLE ABSTRACTION!
Fragments
•  OS	
  can	
  create	
  new	
  
instances	
  you	
  don't	
  
know	
  about	
  
•  Retrieve	
  with	
  
findByTag()	
  
•  Can	
  be	
  a@ached	
  to	
  
one	
  of	
  many	
  AcKviKes	
  
subclass	
  
Fragments / Communication
public class MasterFragment extends Fragment {	
private Listener listener;	
public interface Listener {	
void onItemSelected(int position){}	
	 	onAttach(Activity a) {	
	 	 	super.onAttach(a);	
	 	 	listener = (Listener) a;	
}}	
public class HomeActivity extends Activity	
implements MasterFragment.Listener {	
	onItemSelected(int position) {	
}}
Fragments / Communication
AcKvity1	
  
FragmentA	
   FragmentB	
  
AcKvity2	
  
FragmentC	
  
AcKvity3	
  
FragmentA	
   FragmentD	
  
FragmentC	
  
Fragments / Communication
public class HomeActivity extends Activity	
implements MasterFragment.Listener,	
DetailFragment.Listener,	
PopupFragment.Listener {	
...	
}}	
ListFragment lf =
getFragmentManager().findFragmentByTag("LIST");
if (lf == null) {	
	getActivity().onItemSelected();	
}
Need a Bus
Event Bus
AcKvity1	
  
FragmentA	
  
FragmentB	
  
BUS	
  
AcKvity2	
  
FragmentQ	
  
AcKvity1	
  
Service	
  
Event Bus
AcKvity1	
  
FragmentA	
  
FragmentB	
  
BUS	
  
AcKvity2	
  
FragmentQ	
  
AcKvity1	
  
Service	
  
Excellent	
  library	
  by	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  :	
  OMo	
  
Event Bus
•  Also	
  called	
  Publish	
  /	
  Subscribe	
  in	
  distributed	
  
systems	
  
•  An	
  agent	
  posts	
  a	
  message	
  on	
  an	
  event	
  (topic)	
  
•  All	
  objects	
  registered	
  on	
  that	
  topic,	
  receive	
  
event	
  
•  Decouple	
  caller	
  by	
  receiver	
  +	
  synchronous	
  
delivery	
  
Otto
bus.post(new AnswerAvailableEvent(42));	
bus.register(this);	
...	
@Subscribe public void
answerAvailable(AnswerAvailableEvent event) {	
// TODO: React to the event somehow!	
}	
...	
bus.unregister(this);	
	
static Bus bus = new Bus();
Otto
•  Event	
  delivered!	
  
•  O@o	
  is	
  not	
  rocket	
  science:	
  
•  ReflecKon	
  to	
  spot	
  methods	
  that	
  declare	
  to	
  handle	
  
some	
  object	
  as	
  event	
  
•  search	
  for	
  method	
  that	
  accept	
  some	
  event	
  and	
  
deliver	
  the	
  event	
  
•  More	
  features...	
  
Otto
•  Event	
  delivered!	
  
•  O@o	
  is	
  not	
  rocket	
  science:	
  
•  ReflecKon	
  to	
  spot	
  methods	
  that	
  declare	
  to	
  handle	
  
some	
  object	
  as	
  event	
  
•  search	
  for	
  method	
  that	
  accept	
  some	
  event	
  and	
  
deliver	
  the	
  event	
  
•  More	
  features...	
  
Remember	
  to	
  call	
  unregister()!	
  	
  
Memory	
  (acKvity)	
  leak	
  danger	
  
DIY BUS
DIY Bus
events	
  =	
  integers	
  	
  
parameters	
  =	
  object	
  or	
  integers	
  
	
  
MyObject obj = createMyObject();	
Bus.i().publish(0x2324, obj);	
Bus.i().subscribe(new Invokable() {	
	invoke(int event, Object param) {	
	 	MyObject obj = (MyObject) param;	
	}	
}, 0x2324);
DIY Bus
events	
  =	
  integers	
  	
  
parameters	
  =	
  object	
  or	
  integers	
  
	
  
MyObject obj = createMyObject();	
Bus.i().publish(0x2324, obj);	
Bus.i().subscribe(new Invokable() {	
	invoke(int event, Object param) {	
	 	MyObject obj = (MyObject) param;	
	}	
}, 0x2324);	
Bus.java	
  is	
  90	
  lines	
  of	
  code!	
  
	
  
WRAPPING UP!
BOOST JAVA OBJ-C C# JS
you	
  need	
  to	
  work	
  your	
  way	
  towards	
  asynchronous	
  expressiveness	
  
SIMPLE ABSTRACTIONS
CAN TURN YOUR WORLD
Rx	
  is	
  a	
  simple	
  abstracKon	
  that	
  enables	
  a	
  truckload	
  of	
  features	
  
Event	
  Bus	
  is	
  so	
  old,	
  is	
  so	
  simple,	
  and	
  works	
  so	
  well.	
  
SERVER & CLIENT
scale	
  is	
  orders	
  of	
  magnitude	
  smaller,	
  	
  
different	
  problems,	
  
same	
  techniques.	
  
:)
QUESTIONS&
ANSWERS
Links
•  RxJava	
  by	
  Nejlix	
  
hMps://github.com/NePlix/RxJava/wiki	
  
•  O@o	
  by	
  Square	
  
hMp://square.github.io/oMo	
  
•  DIY	
  Bus	
  
hMps://gist.github.com/hazam/
8ac4abd927588d0bd04b	
  
•  EPFL	
  ReacKve	
  on	
  Coursera	
  
hMps://www.coursera.org/course/reacEve	
  
Grazie.Non	
  dimenKcare	
  di	
  riempire	
  il	
  modulo	
  di	
  feedback	
  
AND02
emanuele.disaverio@gmail.com	
  
hazam	
  
emanueledisaverio	
  

Weitere ähnliche Inhalte

Was ist angesagt?

Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016 Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016 Lluis Franco
 
Code generation with javac plugin
Code generation with javac pluginCode generation with javac plugin
Code generation with javac pluginOleksandr Radchykov
 
Csw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemesCsw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemesCanSecWest
 
.NET Multithreading and File I/O
.NET Multithreading and File I/O.NET Multithreading and File I/O
.NET Multithreading and File I/OJussi Pohjolainen
 
Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with AkkaJohan Andrén
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Daniele Pallastrelli
 
$q and Promises in AngularJS
$q and Promises in AngularJS $q and Promises in AngularJS
$q and Promises in AngularJS a_sharif
 
Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with AkkaJohan Andrén
 
Exploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservicesExploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservices💡 Tomasz Kogut
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Jiayun Zhou
 
Smarter Testing with Spock
Smarter Testing with SpockSmarter Testing with Spock
Smarter Testing with SpockDmitry Voloshko
 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to RxAndrey Cheptsov
 
MicroProfile: A Quest for a Lightweight and Modern Enterprise Java Platform
MicroProfile: A Quest for a Lightweight and Modern Enterprise Java PlatformMicroProfile: A Quest for a Lightweight and Modern Enterprise Java Platform
MicroProfile: A Quest for a Lightweight and Modern Enterprise Java PlatformMike Croft
 
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...EPAM_Systems_Bulgaria
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixTracy Lee
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Ontico
 

Was ist angesagt? (20)

Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016 Async best practices DotNet Conference 2016
Async best practices DotNet Conference 2016
 
Code generation with javac plugin
Code generation with javac pluginCode generation with javac plugin
Code generation with javac plugin
 
Csw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemesCsw2016 gawlik bypassing_differentdefenseschemes
Csw2016 gawlik bypassing_differentdefenseschemes
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 
.NET Multithreading and File I/O
.NET Multithreading and File I/O.NET Multithreading and File I/O
.NET Multithreading and File I/O
 
Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with Akka
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
$q and Promises in AngularJS
$q and Promises in AngularJS $q and Promises in AngularJS
$q and Promises in AngularJS
 
Firebase ng2 zurich
Firebase ng2 zurichFirebase ng2 zurich
Firebase ng2 zurich
 
Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with Akka
 
Exploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservicesExploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservices
 
Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015Akka Cluster in Java - JCConf 2015
Akka Cluster in Java - JCConf 2015
 
Smarter Testing with Spock
Smarter Testing with SpockSmarter Testing with Spock
Smarter Testing with Spock
 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to Rx
 
MicroProfile: A Quest for a Lightweight and Modern Enterprise Java Platform
MicroProfile: A Quest for a Lightweight and Modern Enterprise Java PlatformMicroProfile: A Quest for a Lightweight and Modern Enterprise Java Platform
MicroProfile: A Quest for a Lightweight and Modern Enterprise Java Platform
 
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
 
RxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMixRxJS Operators - Real World Use Cases - AngularMix
RxJS Operators - Real World Use Cases - AngularMix
 
NestJS
NestJSNestJS
NestJS
 
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
Паразитируем на React-экосистеме (Angular 4+) / Алексей Охрименко (IPONWEB)
 

Ähnlich wie Asynchronous Techniques for Android Programming

The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsIntegration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsBizTalk360
 
Seastar @ NYCC++UG
Seastar @ NYCC++UGSeastar @ NYCC++UG
Seastar @ NYCC++UGAvi Kivity
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade ServerlessKatyShimizu
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade ServerlessKatyShimizu
 
UniRx - Reactive Extensions for Unity(EN)
UniRx - Reactive Extensions for Unity(EN)UniRx - Reactive Extensions for Unity(EN)
UniRx - Reactive Extensions for Unity(EN)Yoshifumi Kawai
 
2018 05-16 Evolving Technologies: React, Babel & Webpack
2018 05-16 Evolving Technologies: React, Babel & Webpack2018 05-16 Evolving Technologies: React, Babel & Webpack
2018 05-16 Evolving Technologies: React, Babel & WebpackCodifly
 
Demystifying Reactive Programming
Demystifying Reactive ProgrammingDemystifying Reactive Programming
Demystifying Reactive ProgrammingTom Bulatewicz, PhD
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejsJames Carr
 
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...Sencha
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav DukhinFwdays
 
Playing With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.jsPlaying With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.jsMike Hagedorn
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every dayVadym Khondar
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 
Nanocloud cloud scale jvm
Nanocloud   cloud scale jvmNanocloud   cloud scale jvm
Nanocloud cloud scale jvmaragozin
 

Ähnlich wie Asynchronous Techniques for Android Programming (20)

JavaFX Pitfalls
JavaFX PitfallsJavaFX Pitfalls
JavaFX Pitfalls
 
React native
React nativeReact native
React native
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Celery with python
Celery with pythonCelery with python
Celery with python
 
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsIntegration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
 
Seastar @ NYCC++UG
Seastar @ NYCC++UGSeastar @ NYCC++UG
Seastar @ NYCC++UG
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
 
UniRx - Reactive Extensions for Unity(EN)
UniRx - Reactive Extensions for Unity(EN)UniRx - Reactive Extensions for Unity(EN)
UniRx - Reactive Extensions for Unity(EN)
 
2018 05-16 Evolving Technologies: React, Babel & Webpack
2018 05-16 Evolving Technologies: React, Babel & Webpack2018 05-16 Evolving Technologies: React, Babel & Webpack
2018 05-16 Evolving Technologies: React, Babel & Webpack
 
Demystifying Reactive Programming
Demystifying Reactive ProgrammingDemystifying Reactive Programming
Demystifying Reactive Programming
 
Introduction to nodejs
Introduction to nodejsIntroduction to nodejs
Introduction to nodejs
 
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
SenchaCon 2016: Advanced Techniques for Buidling Ext JS Apps with Electron - ...
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
 
Playing With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.jsPlaying With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.js
 
Ondemand scaling-aws
Ondemand scaling-awsOndemand scaling-aws
Ondemand scaling-aws
 
Reactive programming every day
Reactive programming every dayReactive programming every day
Reactive programming every day
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Nanocloud cloud scale jvm
Nanocloud   cloud scale jvmNanocloud   cloud scale jvm
Nanocloud cloud scale jvm
 

Mehr von Emanuele Di Saverio

Mehr von Emanuele Di Saverio (6)

In a Material world
In a Material worldIn a Material world
In a Material world
 
The Android Experience
The Android ExperienceThe Android Experience
The Android Experience
 
Android 1.5 to 3.0: a compatibility journey
Android 1.5 to 3.0: a compatibility journeyAndroid 1.5 to 3.0: a compatibility journey
Android 1.5 to 3.0: a compatibility journey
 
Dive Into Android [ITA] - Long
Dive Into Android [ITA] - LongDive Into Android [ITA] - Long
Dive Into Android [ITA] - Long
 
Whymca Dive into Android [ITA]
Whymca Dive into Android [ITA]Whymca Dive into Android [ITA]
Whymca Dive into Android [ITA]
 
Android Bluetooth Hacking Java Day2010 Eng
Android Bluetooth Hacking Java Day2010 EngAndroid Bluetooth Hacking Java Day2010 Eng
Android Bluetooth Hacking Java Day2010 Eng
 

Asynchronous Techniques for Android Programming

  • 1. ASYNCHRONOUS TECHNIQUES FOR ANDROID: PROGRAMMING SIDEWAYS Emanuele  Di  Saverio   AND02                    Senior  Design  Technologist   emanuele.disaverio@gmail.com   hazam   emanueledisaverio  
  • 2. Who? "Agilist  in  a  Mobile  world"   •  Mobile  Java  Developer  (2007)   •  Android  (2009)   •  h@p://www.androidavanzato.it  (2012)   Emanuele  Di  Saverio   Senior  Design  Technologist  @  
  • 5. A (biased) TALE ABOUT SERVERS
  • 6. The S in C/S •  a  server  program  in  (connected)  network  compuKng   while (true) { connect(); read(); parseRequest(); process(); sendResponse(); disconnect(); }
  • 7. MPM Prefork (Apache) •  let's   fork() and  spawn  more  processes!   full  address  space  copied  around  
  • 8. MPM Worker (Apache) •  handful  processes  spawn  a  set  of  threads   context  switch  +  shared  memory  
  • 9. Multi Threaded Programming •  Memory  is  shared,  threads  spawn  faster   •  Problem  of  synchronizaKon  –  solvable  only   with  low-­‐level  OS  primiKves  
  • 10. Multi Threaded Programming •  Memory  is  shared,  threads  spawn  faster   •  Problem  of  synchronizaKon  –  solvable  only   with  low-­‐level  OS  primiKves   "…  non-­‐trivial  mul/-­‐threaded  programs  are  incomprehensible  to   humans.  It  is  true  that  the  programming  model  can  be   improved:  design  pa;erns,  atomicity,  improved  languages,  and   formal  methods.  However,  these  techniques  merely  chip  away  at   the  unnecessarily  enormous  non-­‐determinism  of  the  threading   model.  The  model  remains  intrinsically  intractable."   Edward  A.  Lee  –  The  Problem  with  Threads  
  • 13. Context Switch hint:  human  mind  work  same  way  
  • 14. Single Thread Event Old  technique  –  New  Trend   Nginx / NodeJS / Jetty •  User  Code  is  executed  by  single   thread   •  I/O  is  on  separate  thread  
  • 15. MOBILE UI ARCHITECTURE a.k.a.  looked  from  a  high  enough  level,  they're  all  the  same.    
  • 16. The Loop while (true) { processInputEvents(); processSysEvents(); measure(); layout(); draw(); } input   sys   measure   layout   draw   thread  
  • 17.
  • 18. Ok, when it is my turn? HOLLYWOOD  PRINCIPLE   "don't  call  me,  I'll  call  you"   managed  execuKon   LISTENERS   Button a; [...] a.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { //do stuff } }); object  that  receives  callback   through  define  interface   class MyActivity extends Activity { protected void onCreate(Bundle b) { //create my UI } onStop() {...} onStart(){...} }
  • 19. The Android Loop All  callbacks  are  executed  in  Main   Thread!   •  AcKvity   •  Service   •  Fragment   •  animaKons   •  OnClicks   •  OnTouch   •  OnWhatever...      
  • 20. android.os.Looper Make  every  thread  an  event-­‐based  message  queue   public void run() { Looper.prepare(); this.handler = new Handler() handleMessage(int what) if (what == 0x666) //do stuff }; Looper.loop();} handler.sendMessage(Message.obtain(0x666));
  • 21. 60 FPS -> 16 ms per frame ALL YOUR CODE NEEDS TO FIT IN 16ms
  • 22. The Loop while (true) { processInputEvents(); processSysEvents(); measure(); layout(); draw(); } input   sys   measure   layout   draw   thread   16ms  
  • 23. DON'T BLOCK UI THREAD no  network  calls  -­‐  no  file  I/O  -­‐  no  intensive  computaKon  
  • 24. DON'T BLOCK UI THREAD no  network  calls  -­‐  no  file  I/O  -­‐  no  intensive  computaKon   USE OTHER THREADS!
  • 25. Asynchronous Java Java was designed with multithreaded programming in mind (thread ,synchronized, volatile, memory model). Don't rely on that – probabilities you really understand what they do are small. java.util.concurrent.* full of goodies to handle concurrency
  • 26. Asynchronous Java You don't really create Threads that much Executors – Thread pool implemented for you ExecutorService executor = Executors.newFixedThreadPool(10); executor.execute(new Runnable() { run() { //do stuff } });
  • 27. Future – a computation Stock Option Want  to  have  result  of  an  async  execuKon?   Future<String> f1 = executor.submit( new Callable<String> { String call() { return fetchNetworkContent(); } }); f1.get(); //locks current thread
  • 28. Future – combining? Future<String> f1 = executor.submit(...); Future<Integer> f2 = executor.submit(...);   Future<Integer> f3 = executor.submit( new [CALLABLE](f1.get())); Future<Integer> f4 = executor.submit(new [CALLABLE]f2.get()));
  • 29. Future – combining? Future<String> f1 = executor.submit(...); Future<Integer> f2 = executor.submit(...);   Future<Integer> f3 = executor.submit( new [CALLABLE](f1.get())); Future<Integer> f4 = executor.submit(new [CALLABLE]f2.get())); Future<String> f3 = executor.submit(new Callable<String>() { call() { return new CallToRemoteServiceC(f1.get()).call(); }});
  • 30. Future – Other Approaches List<Future> futures = new ArrayList<Future>(); Iterator<Future<?>> i = futures.iterator(); while (i.hasNext()) { Future<?> f = i.next(); if (f.isDone()) { doMoreWork(f.get()); i.remove(); } }
  • 32. Callback Gets  around  the  early  blocking  problem  by  pre-­‐ declaring  the  funcKon  that  will  be  executed   public interface Callback<T> { public void done(T result, Exception error); }
  • 33. Callback fetchNetworkContentAsync(Callback<Response> cb) { mExecutor.execute(new Runnable(){ ... try { Response r = fetchNetworkContent(); cb.done(r, null); } catch(Exception exc) { cb.done(null, exc); }); }
  • 34. WAIT, WE NEED TO BE IN MAIN THREAD
  • 35. Callback fetchNetworkContentAsync(Callback<Response> cb) { mExecutor.execute(new Runnable(){ ... try { Response r = fetchNetworkContent(); mHandler.post(new Runnable() { cb.done(r, null); }); } catch(Exception exc) { cb.done(null, exc); }); }
  • 36. Callback fetchNetworkContentAsync(Callback<Response> cb) { mExecutor.execute(new Runnable(){ ... try { Response r = fetchNetworkContent(); mExecutor.execute(new Runnable() { try { Response r2 = fetchNetworkContent2(r); mHandler.post(new Runnable() { cb.done(r2, null); }); } catch(Exception exc) { cb.done(null, exc); }); }); } catch(Exception exc) { cb.done(null, exc);
  • 37. Callback fetchNetworkContentAsync(Callback<Response> cb) { CountDownLatch cd = new CountDownLatch(2); Response r1, r2; mExecutor.execute(new Runnable(){ Response r1 = fetchNetworkContent(); cd.countDown(); }); mExecutor.execute(new Runnable(){ Response r2 = fetchNetworkContent(); cd.countDown(); }); mExecutor.execute(new Runnable(){ cd.await(); Response r = new Response(r1,r2); mHandler.post(new Runnable() { cb.done(r, null); });
  • 39. Loader & AsyncTask They  help  in  the  field  of  gedng  the  asynchronous  task   happen  in  a  way  that  is  compaKble  with  Android   threading  model,  but  fall  short  in:   •  Combining   •  ManipulaKng   •  Error  Handling   •  Streaming  Data  Handling  
  • 41. NEED MORE ASYNC EXPRESSIVENESS a.k.a.  my  head  is  hurKng  
  • 43.
  • 44. Reactive Extensions (Rx) Developed  by  Microsof  for  .NET  (C#)  2009  –  Erik   Meijer  -­‐  to  support  distributed  cloud  applicaEons     Open  Source  port  of  the  semanKcs  to   •  Rx.ObjC,  RxJava,  Rx.JS,  Scala   •  JavaVM  based  version  is  developed  by  Nejlix  –   Ben  Christensen  
  • 45. Rx (Java): Concepts Observable   EnKty  that  can  fetch  a  stream  of  events   asyncronously,  if  you  subscribe  an  observer  to  it     Observer   You  can  imperaKvely  noKfy  an  observer  about  an   event      
  • 46. Rx (Java): Sample Observable<Employee> loadEmployeeWithName(String name) { Observable<Employee> observable = Observable.create( new Func1<Observer<Employee>, Subscription>() { public Subscription call(final Observer<Employee> observer) { List<Employee> employee = loadEmployeeFromDB(name); for (Employee emp : employees) { try { observer.onNext(emp); } catch (Exception e) { observer.onException(e); }} observer.onCompleted(); return Subscriptions.empty(); } }); return observable;} loadEmployeeWithName("Aldo").subscribe(new Observer<Employee>() { public void onNext(Employee emp) { showEmployeeDataOnScreen(emp); } public void onError(Exception e) { handleException(e); } public void onCompleted() { doneLoadingEmployees(); }});
  • 47. "Async" expressed for collections IMPERATIVE   •  T  next()   •  throws  ExcepKon   •  return   REACTIVE   •  onNext(T)   •  onError(ExcepKon)   •  onCompleted()  
  • 48. Rx (Java): Sample Observable<Employee> loadEmployeeWithName(String name) { Observable<Employee> observable = Observable.create( new Func1<Observer<Employee>, Subscription>() { public Subscription call(final Observer<Employee> observer) { List<Employee> employee = loadEmployeeFromDB(name); for (Employee emp : employees) { try { observer.onNext(emp); } catch (Exception e) { observer.onException(e); }} observer.onCompleted(); return Subscriptions.empty(); } }); return observable;} loadEmployeeWithName("Aldo").subscribe(new Observer<Employee>() { public void onNext(Employee emp) { showEmployeeDataOnScreen(emp); } public void onError(Exception e) { handleException(e); } public void onCompleted() { doneLoadingEmployees(); }}); STILL  SYNC  
  • 49. Rx (Java): Sample Observable<Employee> loadEmployeeWithName(String name) { Observable<Employee> observable = Observable.create( new Func1<Observer<Employee>, Subscription>() { public Subscription call(final Observer<Employee> observer) { executor.postRunnable(new Runnable() {... List<Employee> employee = loadEmployeeFromDB(name); for (Employee emp : employees) { try { mHandler.post(new Runnable() { ... observer.onNext(emp); ... } } catch (Exception e) { mHandler.post(new Runnable() { ... observer.onException(e); }) }} mHandler.post(new Runnable() { observer.onCompleted(); }); return Subscriptions.empty(); } }); }); return observable;}
  • 50. So what is Rx really doing? Rx  is  man-­‐in-­‐the  middle  between  method   calling  and  callback  execuKon     onNext() != onNext() We  have  control  over  scheduling  of  methods    
  • 51. Rx (Java): Sample Observable<Employee> loadEmployeeWithName(String name) { Observable<Employee> observable = Observable.create( new Func1<Observer<Employee>, Subscription>() { public Subscription call(final Observer<Employee> observer) { List<Employee> employee = loadEmployeeFromDB(name); for (Employee emp : employees) { try { observer.onNext(emp); } catch (Exception e) { observer.onException(e); }} observer.onCompleted(); return Subscriptions.empty(); } }); observable = observable.subscribeOn(Schedulers.executor(mExecutor)); observable = observable.observeOn(AndroidScheduler.getInstance()); return observable; }
  • 52. Rx (Java): Sample Observable<Employee> loadEmployeeWithName(String name) { Observable<Employee> observable = Observable.create( new Func1<Observer<Employee>, Subscription>() { public Subscription call(final Observer<Employee> observer) { List<Employee> employee = loadEmployeeFromDB(name); for (Employee emp : employees) { try { observer.onNext(emp); } catch (Exception e) { observer.onException(e); }} observer.onCompleted(); return Subscriptions.empty(); } }); observable = observable.subscribeOn(Schedulers.executor(mExecutor)); observable = observable.observeOn(AndroidScheduler.getInstance()); return observable; } loadEmployeeWithName("Aldo").subscribe(new Observer<Employee>() { public void onNext(Employee emp) { showEmployeeDataOnScreen(emp); }
  • 53. Rx (Java): Features Observer  is  executed  afer  someone  subscribed     Since  we  are  decoupling  method  call  and  callback   execuKon,  we  can  easily  manipulate  and  combine.  
  • 56. Zip Observables Observable.zip(obs1,  obs2,  new  Func1<a,b>()  {});  
  • 57. Rx (Java): Features Each  observer  can  be  scheduled  to  have  his   subscribe  happen  on  any  thread,  and  all   observaKon  happen  on  any  other  thread!   Observable<Image> getImage(URL url) { AtomicBoolean found = new AtomicBoolean(false); obs1 = //get image from in memory cache obs2 = //get image from disc cache obs2 = obs2 .subscribeOn(Schedulers.executor(highprioexecutor)) .observeOn(AndroidScheduler.getInstance()); obs3 = //get image from network obs3 = obs3 .subscribeOn(Schedulers.executor(lowprioexecutor)) .observeOn(AndroidScheduler.getInstance()); return Observable.concat(obs1, obs2, obs3); }
  • 58. Rx (Java): Features Connectable  version  of  Observable  can  be   subscribed  mulKple  Kmes,  doesn't  restart  but  goes   on,  can  even  replay  past  events!   Observable<Tweet> obs = createObservableThatStreamTweet(); void initObs() { obs.replay(); obs = obs.connect(); } Observable<Tweet> getPopularTweets() { return obs; }
  • 59. Reactive the world •  We  could  wrap  all  applicaKon  in  reacKves:     click  events  could  generate  events  that  can  route  to  DB  calls.  View  could   subscribe  to  DB  observable  and...     Yes.  Maybe.  
  • 60.
  • 61. Reactive Over Services <<sync>>   Employee  DAO   <<sync>>   Paypal  API   <<async>>   Android  NFC  Intents   RX  Layer   UI  /  Controllers   FragmentA   FragmentA  AcKvity   Views  
  • 64. Fragments NOT A UI but a LIFECYCLE ABSTRACTION!
  • 65. Fragments •  OS  can  create  new   instances  you  don't   know  about   •  Retrieve  with   findByTag()   •  Can  be  a@ached  to   one  of  many  AcKviKes   subclass  
  • 66. Fragments / Communication public class MasterFragment extends Fragment { private Listener listener; public interface Listener { void onItemSelected(int position){} onAttach(Activity a) { super.onAttach(a); listener = (Listener) a; }} public class HomeActivity extends Activity implements MasterFragment.Listener { onItemSelected(int position) { }}
  • 67. Fragments / Communication AcKvity1   FragmentA   FragmentB   AcKvity2   FragmentC   AcKvity3   FragmentA   FragmentD   FragmentC  
  • 68. Fragments / Communication public class HomeActivity extends Activity implements MasterFragment.Listener, DetailFragment.Listener, PopupFragment.Listener { ... }} ListFragment lf = getFragmentManager().findFragmentByTag("LIST"); if (lf == null) { getActivity().onItemSelected(); }
  • 70. Event Bus AcKvity1   FragmentA   FragmentB   BUS   AcKvity2   FragmentQ   AcKvity1   Service  
  • 71. Event Bus AcKvity1   FragmentA   FragmentB   BUS   AcKvity2   FragmentQ   AcKvity1   Service  
  • 72. Excellent  library  by                                  :  OMo   Event Bus •  Also  called  Publish  /  Subscribe  in  distributed   systems   •  An  agent  posts  a  message  on  an  event  (topic)   •  All  objects  registered  on  that  topic,  receive   event   •  Decouple  caller  by  receiver  +  synchronous   delivery  
  • 73. Otto bus.post(new AnswerAvailableEvent(42)); bus.register(this); ... @Subscribe public void answerAvailable(AnswerAvailableEvent event) { // TODO: React to the event somehow! } ... bus.unregister(this); static Bus bus = new Bus();
  • 74. Otto •  Event  delivered!   •  O@o  is  not  rocket  science:   •  ReflecKon  to  spot  methods  that  declare  to  handle   some  object  as  event   •  search  for  method  that  accept  some  event  and   deliver  the  event   •  More  features...  
  • 75. Otto •  Event  delivered!   •  O@o  is  not  rocket  science:   •  ReflecKon  to  spot  methods  that  declare  to  handle   some  object  as  event   •  search  for  method  that  accept  some  event  and   deliver  the  event   •  More  features...   Remember  to  call  unregister()!     Memory  (acKvity)  leak  danger  
  • 77. DIY Bus events  =  integers     parameters  =  object  or  integers     MyObject obj = createMyObject(); Bus.i().publish(0x2324, obj); Bus.i().subscribe(new Invokable() { invoke(int event, Object param) { MyObject obj = (MyObject) param; } }, 0x2324);
  • 78. DIY Bus events  =  integers     parameters  =  object  or  integers     MyObject obj = createMyObject(); Bus.i().publish(0x2324, obj); Bus.i().subscribe(new Invokable() { invoke(int event, Object param) { MyObject obj = (MyObject) param; } }, 0x2324); Bus.java  is  90  lines  of  code!    
  • 80. BOOST JAVA OBJ-C C# JS you  need  to  work  your  way  towards  asynchronous  expressiveness  
  • 81. SIMPLE ABSTRACTIONS CAN TURN YOUR WORLD Rx  is  a  simple  abstracKon  that  enables  a  truckload  of  features   Event  Bus  is  so  old,  is  so  simple,  and  works  so  well.  
  • 82. SERVER & CLIENT scale  is  orders  of  magnitude  smaller,     different  problems,   same  techniques.  
  • 83. :)
  • 85. Links •  RxJava  by  Nejlix   hMps://github.com/NePlix/RxJava/wiki   •  O@o  by  Square   hMp://square.github.io/oMo   •  DIY  Bus   hMps://gist.github.com/hazam/ 8ac4abd927588d0bd04b   •  EPFL  ReacKve  on  Coursera   hMps://www.coursera.org/course/reacEve  
  • 86. Grazie.Non  dimenKcare  di  riempire  il  modulo  di  feedback   AND02 emanuele.disaverio@gmail.com   hazam   emanueledisaverio