All people know that it's big problem work out of UI thread. we found different ways to do it, from Android asynctask or Services to external libraries based on Reactive Manifesto as RxJava or JDeferred. This new paradigmas going to make all asynchronous but is this the best solution?
28. Mem Cache
We can coordinate promises.
DeferredManager dm = new DefaultDeferredManager();
Promise p1, p2, p3;
// initialize p1, p2, p3
dm.when(p1, p2, p3)
.done(âŠ)
.fail(âŠ)
29. Mem Cache
We can do operations over the promises.
Filter (Map)
Deferred d = âŠ;
Promise p = d.promise();
Promise ïŹltered = p.then(new DoneFilter<Integer, Integer>() {
public Integer ïŹlterDone(Integer result)
return result * 10;
}
});
31. Mem Cache
Combination of the best ideas from
the Observer pattern, the Iterator pattern,
and functional programming
32. Mem Cache
You need to change your mind. You
âobserveâ changes, donât invoke it.
33. A lot of âOperatorsâ that allow
combination observables.
Example: Zip
34. You decide in which thread your
observable will emit the stream of data
(onNext() on Subscriber).
.subscribeOn(Schedulers.newThread())
You deciden in which thread Observables
will execute their job.
.observeOn(AndroidSchedulers.mainThread())
35.
36. It has a steep learning curve. Take time to
understand the paradigma change and
learn the operators.
42. Mem Cache
Be careful with the number of threads
that your are creating. A big number of
threads can overkill the system.
43. Mem Cache
I donât appreciate a big impact to the
memory or to garbage collector.
44. Adam Tornhill
âSometimes abstraction and
encapsulation are at odds with
performance â although not nearly as
often as many developers believe â but
it is always a good practice ïŹrst to make
your code right, and then make it fast.â
Brian Goetz.
Java Concurrency in Prac.
45. Readability is the must important thing
always
Observable<List<User>> usersDb =
db.getUsers().subscribeOn(Schedulers.newThread());
Observable<List<User>> users =
apiRest.getUsers().subscribeOn(Schedulers.newThread());
Observable<Map<String, Integer>> likeCountPerUser = apiRest.getLikes();
Observable<List<User>> obs = Observable.zip(users, likeCountPerUser, (users, likes) ->
{
for (User user: users) {
if (likes.containsKey(user.getId())) {
user.setNumLikes (likes.get(user.getId()));
}
}
return users;
}).merge(userDb);
return obs;
46. Readability is the must important thing
always
List<User> usersDb = db.getUsers();
List<User> users = apiRest.getUsers();
Map<String, Integer> likeCountPerUser = apiRest.getLikes();
for (User user: users) {
if (likes.containsKey(user.getId())) {
user.setNumLikes(likes.get(user.getId()));
}
}
List<User> usersFinal = new ArrayList<>();
usersFinal.addAll(users);
usersFinal.addAll(usersDb);
return usersFinal;
47. Bibliography
Theyâre the cracks!
Java Concurrency in Prac. Brian Goetz
http://www.reactivemanifesto.org/
http://reactivex.io/
https://github.com/jdeferred/jdeferred
https://github.com/BoltsFramework/Bolts-Android
https://github.com/android10/frodo
https://github.com/ïŹipper83/trabajando-en-diferido
Thanks to Pedro V Gomez, Fernando Cejas, Nuria Ruiz and karumi
team for their support.
48. Find meI am very social!
jorge@karumi.com
@ïŹipper83
+JorgeJBarroso
Questions?