Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Get out of my thread (Trabajando en diferido)

1,629 views

Published on

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?

Published in: Engineering

Get out of my thread (Trabajando en diferido)

  1. 1. Programando en diferido (Get out of my thread) Jorge Juan Barroso Carmona jorge@karumi.com @flipper83 +JorgeJBarroso Android expert
  2. 2. Basho. Poet Karumi is the beauty of ordinary things spoken of in a simple way.
  3. 3. Miguel Noguera
  4. 4. Reactive Manifesto
  5. 5. Mem CacheResponsive
  6. 6. Mem CacheResilient
  7. 7. Mem CacheElastic
  8. 8. Mem CacheMessage Driven
  9. 9. Mem Cache The manifesto is not concrete
  10. 10. Adam Tornhill “First, solve the problem. Then, write the code.” John Johnson.
  11. 11. Adam Tornhill
  12. 12. Async Task
  13. 13. Mem Cache Really close to the UI Thread and to the framework.
  14. 14. Mem Cache 
 task.execute()
 vs
 task.executeOnExecutor (AsyncTask.THREAD_POOL_EXECUTOR)
  15. 15. Mem Cache 
 Problems changing orientation.
  16. 16. Mem Cache
  17. 17. Priority Job Queue
  18. 18. Mem CacheProducer - consumer approach.
  19. 19. Mem CachePriorities, group, delay, store … jobs
  20. 20. Mem CacheNeed consumers configuration.
  21. 21. Mem Cache
  22. 22. Mem Cache We need to find a way to notify responders. Buses? callbacks? post on handler?
  23. 23. Mem Cache “Callback hell” if we need coordinate a lot of Jobs.
  24. 24. Promises (JDeferred)
  25. 25. Mem CacheFuture and Promises
  26. 26. Mem Cache We can choose execution and responses thread with ExecutionServices and ExecutionScope.
  27. 27. Mem Cache
  28. 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. 29. 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; } });
  30. 30. RxJava
  31. 31. Mem Cache Combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming
  32. 32. Mem Cache You need to change your mind. You “observe” changes, don’t invoke it.
  33. 33. A lot of “Operators” that allow combination observables. Example: Zip
  34. 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. 35. It has a steep learning curve. Take time to understand the paradigma change and learn the operators.
  36. 36. Debugging can be hell. Frodo is your friend.
  37. 37. Cold vs Hot observables Defer vs Publish
  38. 38. Karumi Recommends
  39. 39. Mem CacheGet out from UI thread ASAP
  40. 40. Mem Cache Apps are really dependent on state. Blocking threads.
  41. 41. Mem Cache Be careful with the number of threads that your are creating. A big number of threads can overkill the system.
  42. 42. Mem Cache I don’t appreciate a big impact to the memory or to garbage collector.
  43. 43. 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.
  44. 44. 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;
  45. 45. 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;
  46. 46. 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.
  47. 47. Find meI am very social! jorge@karumi.com @flipper83 +JorgeJBarroso Questions?

×