get out of my thread (trabajando en diferido)
TRANSCRIPT
![Page 1: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/1.jpg)
Programando en diferido(Get out of my thread)Jorge Juan Barroso Carmona
[email protected]@flipper83+JorgeJBarroso
Android expert
![Page 2: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/2.jpg)
Basho. Poet
Karumi is the beauty of ordinary things spoken of in a simple way.
![Page 3: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/3.jpg)
Miguel Noguera
![Page 4: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/4.jpg)
Reactive Manifesto
![Page 5: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/5.jpg)
Mem CacheResponsive
![Page 6: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/6.jpg)
Mem CacheResilient
![Page 7: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/7.jpg)
Mem CacheElastic
![Page 8: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/8.jpg)
Mem CacheMessage Driven
![Page 9: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/9.jpg)
Mem CacheThe manifesto is not concrete
![Page 10: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/10.jpg)
Adam Tornhill
“First, solve the problem. Then, write the code.”
John Johnson.
![Page 11: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/11.jpg)
Adam Tornhill
![Page 12: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/12.jpg)
Async Task
![Page 13: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/13.jpg)
Mem CacheReally close to the UI Thread and to the framework.
![Page 14: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/14.jpg)
Mem Cache
task.execute()
vs task.executeOnExecutor
(AsyncTask.THREAD_POOL_EXECUTOR)
![Page 15: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/15.jpg)
Mem Cache
Problems changing orientation.
![Page 16: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/16.jpg)
Mem Cache
![Page 17: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/17.jpg)
Priority Job Queue
![Page 18: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/18.jpg)
Mem CacheProducer - consumer approach.
![Page 19: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/19.jpg)
Mem CachePriorities, group, delay, store … jobs
![Page 20: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/20.jpg)
Mem CacheNeed consumers configuration.
![Page 21: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/21.jpg)
Mem Cache
![Page 22: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/22.jpg)
Mem CacheWe need to find a way to notify
responders.Buses?
callbacks?post on handler?
![Page 23: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/23.jpg)
Mem Cache“Callback hell” if we need coordinate a lot of Jobs.
![Page 24: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/24.jpg)
Promises(JDeferred)
![Page 25: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/25.jpg)
Mem CacheFuture and Promises
![Page 26: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/26.jpg)
Mem CacheWe can choose execution and responses
thread with ExecutionServices and ExecutionScope.
![Page 27: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/27.jpg)
Mem Cache
![Page 28: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/28.jpg)
Mem Cache
We can coordinate promises.
DeferredManager dm = new DefaultDeferredManager();Promise p1, p2, p3;// initialize p1, p2, p3dm.when(p1, p2, p3) .done(…) .fail(…)
![Page 29: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/29.jpg)
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; }});
![Page 30: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/30.jpg)
RxJava
![Page 31: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/31.jpg)
Mem CacheCombination of the best ideas from
the Observer pattern, the Iterator pattern, and functional programming
![Page 32: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/32.jpg)
Mem CacheYou need to change your mind. You “observe” changes, don’t invoke it.
![Page 33: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/33.jpg)
A lot of “Operators” that allow combination observables.
Example: Zip
![Page 34: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/34.jpg)
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())
![Page 35: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/35.jpg)
![Page 36: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/36.jpg)
It has a steep learning curve. Take time to understand the paradigma change and
learn the operators.
![Page 37: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/37.jpg)
Debugging can be hell. Frodo is your friend.
![Page 38: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/38.jpg)
Cold vs Hot observablesDefer vs Publish
![Page 39: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/39.jpg)
Karumi Recommends
![Page 40: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/40.jpg)
Mem CacheGet out from UI thread ASAP
![Page 41: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/41.jpg)
Mem CacheApps are really dependent on state.Blocking threads.
![Page 42: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/42.jpg)
Mem CacheBe careful with the number of threads that your are creating. A big number of
threads can overkill the system.
![Page 43: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/43.jpg)
Mem CacheI don’t appreciate a big impact to the memory or to garbage collector.
![Page 44: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/44.jpg)
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.
![Page 45: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/45.jpg)
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;
![Page 46: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/46.jpg)
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;
![Page 47: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/47.jpg)
BibliographyThey’re the cracks!
Java Concurrency in Prac. Brian Goetzhttp://www.reactivemanifesto.org/http://reactivex.io/https://github.com/jdeferred/jdeferredhttps://github.com/BoltsFramework/Bolts-Androidhttps://github.com/android10/frodohttps://github.com/flipper83/trabajando-en-diferidoThanks to Pedro V Gomez, Fernando Cejas, Nuria Ruiz and karumi team for their support.
![Page 49: Get out of my thread (Trabajando en diferido)](https://reader033.vdocuments.us/reader033/viewer/2022052117/58cee5fe1a28ab333d8b51c9/html5/thumbnails/49.jpg)