Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Get out of my thread (Programando en diferido)

Get out of my thread (Programando en diferido)

One big problem in Android working with Android is to implement long time running tasks. In this talk we review and compare some libraries to resolve this problem. AnsyncTask? RxJava? Agera? JDeferred? which is the winner? perfomance issues? learn curve? when use each one?

Karumi

June 21, 2016
Tweet

More Decks by Karumi

Other Decks in Technology

Transcript

  1. Programando en diferido (Get out of my thread) Jorge Juan

    Barroso Carmona [email protected] @flipper83 +JorgeJBarroso Android expert android GDE
  2. Mem Cache We need to find a way to notify

    responders. Buses? callbacks? post on handler?
  3. Mem Cache We can choose execution and responses thread with

    ExecutionServices and ExecutionScope.
  4. 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(…)
  5. Mem Cache We can do operations over the promises. Filter

    (Map) Deferred d = …; Promise p = d.promise(); Promise filtered = p.then(new DoneFilter<Integer, Integer>() { public Integer filterDone(Integer result) return result * 10; } });
  6. Mem Cache Agera is a set of classes and interfaces

    to help write functional, asynchronous, and reactive applications for Android.
  7. Mem Cache Only have a few composition methods. Merge and

    transform. Difficult coordinate more than two observables.
  8. Mem Cache Combination of the best ideas from the Observer

    pattern, the Iterator pattern, and functional programming
  9. 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())
  10. It has a steep learning curve. Take time to understand

    the paradigma change and learn the operators.
  11. Mem Cache Be careful with the number of threads that

    your are creating. A big number of threads can overkill the system.
  12. Mem Cache I don’t appreciate a big impact to the

    memory or to garbage collector.
  13. 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 first to make your code right, and then make it fast.” Brian Goetz. Java Concurrency in Prac.
  14. 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;
  15. 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;
  16. 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/flipper83/trabajando-en-diferido Thanks to Pedro V Gomez, Fernando Cejas, Nuria Ruiz and karumi team for their support.