Get out of my thread (Trabajando en diferido)
-
Upload
jorge-barroso -
Category
Engineering
-
view
1.584 -
download
0
Transcript of 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
Basho. Poet
Karumi is the beauty of ordinary things spoken of in a simple way.
Miguel Noguera
Reactive Manifesto
Mem CacheResponsive
Mem CacheResilient
Mem CacheElastic
Mem CacheMessage Driven
Mem CacheThe manifesto is not concrete
Adam Tornhill
“First, solve the problem. Then, write the code.”
John Johnson.
Adam Tornhill
Async Task
Mem CacheReally close to the UI Thread and to the framework.
Mem Cache
task.execute()
vs task.executeOnExecutor
(AsyncTask.THREAD_POOL_EXECUTOR)
Mem Cache
Problems changing orientation.
Mem Cache
Priority Job Queue
Mem CacheProducer - consumer approach.
Mem CachePriorities, group, delay, store … jobs
Mem CacheNeed consumers configuration.
Mem Cache
Mem CacheWe need to find a way to notify
responders.Buses?
callbacks?post on handler?
Mem Cache“Callback hell” if we need coordinate a lot of Jobs.
Promises(JDeferred)
Mem CacheFuture and Promises
Mem CacheWe can choose execution and responses
thread with ExecutionServices and ExecutionScope.
Mem Cache
Mem Cache
We can coordinate promises.
DeferredManager dm = new DefaultDeferredManager();Promise p1, p2, p3;// initialize p1, p2, p3dm.when(p1, p2, p3) .done(…) .fail(…)
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; }});
RxJava
Mem CacheCombination of the best ideas from
the Observer pattern, the Iterator pattern, and functional programming
Mem CacheYou need to change your mind. You “observe” changes, don’t invoke it.
A lot of “Operators” that allow combination observables.
Example: Zip
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())
It has a steep learning curve. Take time to understand the paradigma change and
learn the operators.
Debugging can be hell. Frodo is your friend.
Cold vs Hot observablesDefer vs Publish
Karumi Recommends
Mem CacheGet out from UI thread ASAP
Mem CacheApps are really dependent on state.Blocking threads.
Mem CacheBe careful with the number of threads that your are creating. A big number of
threads can overkill the system.
Mem CacheI don’t appreciate a big impact to the memory or to garbage collector.
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.
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;
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;
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.