Introduction à RxJava - Tutos Android France Une des grandes problématiques des développeurs Android est le traitement des taches longues, dites asynchrones. Les petites mains de Google nous avaient pour cela fourni des AsyncTask, qui ont un comportement très limité et une utilisation très austère. En 2015 ce mot est à bannir du dictionnaire « Androidophone » Oubliez la définitivement, passez à RxJava ! Asynchrone La plupart des tâches « lourdes » sous Android sont à effectuer dans un Thread différent de l’affichage graphique. Parmi ces tâches nous avons principalement des appels réseau, de la lecture de fichiers, ou encore des algorithmes assez lourds comme un tri, une recherche, etc. Si vous avez l’habitude d’utiliser Retrofit ou toute autre librairie, vous cachez vos traitements derrière des Listener ou Callbacks : Cette méthode fonctionne très bien, le seul reproche qu’on peut lui faire est l’enchaînement des appels, qui va s’avérer un peu lourd. Reactive Programming / Observer Pattern Opérations longues / Observable Par exemple :
Android - Oubliez définitivement les AsyncTask avec RxJava - Publicis Sapient Engineering - Engineering Done Right En tant que développeur Android, vous avez sûrement déjà été confronté aux limitations des AsyncTasks. Peut-être avez-vous eu l’occasion de saisir dans votre moteur de recherche préféré les mots-clés "Orientation Change", "Memory Leak", "Error Handling" ou même "Running Parallel" suivis du célèbre "AsyncTask" ? Si c’est le cas, lisez ce qui suit puisque nous allons vous montrer une nouvelle façon d’exécuter des traitements (par exemple des appels réseau) sur un thread séparé, tout en récupérant les résultats dans le thread principal, le tout sans parler de Thread, de Service, de Fragment, d’EventBus ou d’AsyncTask. À la place, nous allons vous présenter RxJava. Introduction à RxJava RxJava est une implémentation réalisée par Netflix du projet Rx (Reactive Extensions) initialement conçu par Microsoft, et libéré en novembre 2012. Rx est basé sur le paradigme de la programmation réactive fonctionnelle (Functional Reactive Programming) …Et concrètement… ? https: Il renvoie un résultat de ce type :
RxFlux Android Architecture. This is the first article about the new… | by Marcel Pintó | The Startup | Medium This is the first article about the new RxFlux framework. Is small light-weight framework that makes it easy to follow the Flux pattern with RxJava functionalities. I will not get into the details on how Flux works, in order to understand more how to use Flux pattern in Android, @lgvalle created a nice example and explanation. I would recommend to read it before. In this article I will explain how to create an app from scratch using RxFlux. Please take the sample app in the repo as a reference. In any normal app development the first step is to think what do you want to do. Let’s imagine that we want to create a simple app that uses the Github API. First thing we must think is which “Stores” do we need. RepositoriesStore: will contain the list of GitHubRepos.UsersStore: will contain the list of users we request for each repo. According to the diagram below, now we need to think what interface shall we define in order that the view gets the needed information. GitUser getUser(String id);
notes: RX Java with Kotlin in Baby Steps w/ Annyce Davis | by Ataul Munim | ProAndroidDev (2:40) RX is about programming with asynchronous data streams. These streams can come from anywhere: user click events, push notifications and GPS updates for example. (4:45) You can use RX in many places. Even if there’s only one event (e.g. a response from a network request), many events (e.g. user typing in a text field), or an error (such that you get zero data events) — they can all be modelled as asynchronous data streams. (6:15) RX is a tool that facilitates lots of things, including: chaining — start operations based on the result of an earlier operationthreading — schedule work and delivery of events on different threads without much effortcomposition & data transformation — combine smaller functions together to achieve goals (8:45) It’s similar to code you’ll see in Kotlin. This example in Kotlin demonstrates some standard operators. Conversely, RX waits until there’s a subscriber, and applies operations on each emission as and when they come (“lazy”). You have available to you:
proandroiddev When RxJava came to the world the Event bus pattern implementation becomes much easier, but we still need to write abstraction on top of the RxJava to make type safety API for our EventBus. First of all, create an EventBus interface. EventBus A class implements the BusEvent interface to indicate EventBus event. Queue For each type of event, we need to create Queue . The consumeReplayQueue method fire cached event and clear stream by publishing internal IdleEvent that will be filtered. Stream To get stream we need to call queue the method of EventBus interface and passe one of the queues that we created earlier. And finally a quick example of how to use out EventBus: with replay stream Thanks for the reading, all code you can find here.
Completo artículo sobre Programación Reactiva, desde sus conceptos básicos, empleando RxJS. Resumen del videocurso publicado en Egghead.io, titulado "Introduction to Reactive Programming". by alce65 Jan 19