Get out of my thread (Trabajando en diferido)

49
Programando en diferido (Get out of my thread) Jorge Juan Barroso Carmona [email protected] @flipper83 +JorgeJBarroso Android expert

Transcript of Get out of my thread (Trabajando en diferido)

Page 1: Get out of my thread (Trabajando en diferido)

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)

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)

Miguel Noguera

Page 4: Get out of my thread (Trabajando en diferido)

Reactive Manifesto

Page 5: Get out of my thread (Trabajando en diferido)

Mem CacheResponsive

Page 6: Get out of my thread (Trabajando en diferido)

Mem CacheResilient

Page 7: Get out of my thread (Trabajando en diferido)

Mem CacheElastic

Page 8: Get out of my thread (Trabajando en diferido)

Mem CacheMessage Driven

Page 9: Get out of my thread (Trabajando en diferido)

Mem CacheThe manifesto is not concrete

Page 10: Get out of my thread (Trabajando en diferido)

Adam Tornhill

“First, solve the problem. Then, write the code.”

John Johnson.

Page 11: Get out of my thread (Trabajando en diferido)

Adam Tornhill

Page 12: Get out of my thread (Trabajando en diferido)

Async Task

Page 13: Get out of my thread (Trabajando en diferido)

Mem CacheReally close to the UI Thread and to the framework.

Page 14: Get out of my thread (Trabajando en diferido)

Mem Cache

task.execute()

vs task.executeOnExecutor

(AsyncTask.THREAD_POOL_EXECUTOR)

Page 15: Get out of my thread (Trabajando en diferido)

Mem Cache

Problems changing orientation.

Page 16: Get out of my thread (Trabajando en diferido)

Mem Cache

Page 17: Get out of my thread (Trabajando en diferido)

Priority Job Queue

Page 18: Get out of my thread (Trabajando en diferido)

Mem CacheProducer - consumer approach.

Page 19: Get out of my thread (Trabajando en diferido)

Mem CachePriorities, group, delay, store … jobs

Page 20: Get out of my thread (Trabajando en diferido)

Mem CacheNeed consumers configuration.

Page 21: Get out of my thread (Trabajando en diferido)

Mem Cache

Page 22: Get out of my thread (Trabajando en diferido)

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)

Mem Cache“Callback hell” if we need coordinate a lot of Jobs.

Page 24: Get out of my thread (Trabajando en diferido)

Promises(JDeferred)

Page 25: Get out of my thread (Trabajando en diferido)

Mem CacheFuture and Promises

Page 26: Get out of my thread (Trabajando en diferido)

Mem CacheWe can choose execution and responses

thread with ExecutionServices and ExecutionScope.

Page 27: Get out of my thread (Trabajando en diferido)

Mem Cache

Page 28: Get out of my thread (Trabajando en diferido)

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)

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)

RxJava

Page 31: Get out of my thread (Trabajando en diferido)

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)

Mem CacheYou need to change your mind. You “observe” changes, don’t invoke it.

Page 33: Get out of my thread (Trabajando en diferido)

A lot of “Operators” that allow combination observables.

Example: Zip

Page 34: Get out of my thread (Trabajando en diferido)

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)
Page 36: Get out of my thread (Trabajando en diferido)

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)

Debugging can be hell. Frodo is your friend.

Page 38: Get out of my thread (Trabajando en diferido)

Cold vs Hot observablesDefer vs Publish

Page 39: Get out of my thread (Trabajando en diferido)

Karumi Recommends

Page 40: Get out of my thread (Trabajando en diferido)

Mem CacheGet out from UI thread ASAP

Page 41: Get out of my thread (Trabajando en diferido)

Mem CacheApps are really dependent on state.Blocking threads.

Page 42: Get out of my thread (Trabajando en diferido)

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)

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)

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)

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)

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)

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 48: Get out of my thread (Trabajando en diferido)

Find meI am very social!

[email protected]@flipper83+JorgeJBarroso

Questions?

Page 49: Get out of my thread (Trabajando en diferido)