Introduction to RxJava4:04 with Jamie Huson
RxJava is a library of code used to handle data using a push and pull mechanism. Just like a notification on your phone, some data will be pushed to a receiver of that data. It’s a different way to think about existing coding patterns you know.
Hi, my name's Jamie Huson, and I'm an Android developer at Etsy. 0:04 In this workshop, we're going to get you started using RX Java. 0:09 We'll cover the concepts and 0:13 API's by integrating RX Java into a familiar project, a toDoList app. 0:15 Let's dive right in. 0:20 RX Java is a library of code to handle data using a push and pull mechanism. 0:22 Just like a notification on your phone, 0:27 some data will be pushed to receiver of that data. 0:29 It's a different way of thinking about existing coding patterns you know. 0:33 For instance, let's take a look at this array of to-do's. 0:37 In your app, you might have a list or array of to-do items. 0:41 Usually, you'll [SOUND] iterate over those items using an index starting at zero and 0:44 moving to the last item. 0:49 Each time you iterate, you'll perform some action with that item. 0:50 In our next Java, we think of these items as being pushed to us one at a time. 0:55 Over time, each item is emitted and we can handle that item as we did before. 1:00 This is very similar to a callback like on a click in Android, 1:05 where we receive a notification when an item is tapped on the screen. 1:09 In this model our data is being provided as it becomes available. 1:13 In our next job the object that emits items is [SOUND] called an Observable. 1:18 The callback that receives items [SOUND] is called the Observer. 1:23 And Observer subscribes to the observables and starts receiving data and 1:27 un-subscribes and the data is no longer received. 1:31 Here we can see the Observable emit items [SOUND] and 1:35 as they're emitted they're passed along to the Observer. 1:39 Here's what that would look like in RX Java code. 1:45 Observable.just creates an Observable that will emit each item from our data set. 1:48 In this case a todoList [SOUND]. 1:53 Now, we've subscribed to the Observable. 1:56 An observer's past of the subscribe method. 2:00 It's type to the kind of data being emitted. 2:02 In this case a Todo object. 2:05 Observer contains the callbacks required for subscription. 2:08 On next is called for each item emitted. 2:12 Uncompleted is called when the Observable has no more items to admit. 2:15 Finally, on errors call if an error occurs. 2:21 It's important to note that on completed and on error are only ever called once. 2:24 And after they are called, on next will not be called again. 2:29 [SOUND] Calling subscriber turns back a subscription object. 2:32 [SOUND] With our subscription we can later call unsubscribe. 2:37 For instance, in on passive our activity in Android. 2:41 These are the basics of the RX Java model. 2:46 Data is submitted from Observables which are subscribed to by Observers. 2:49 Data is pushed to the observables as it becomes available. 2:53 If you've worked with loaders on Android before, 2:57 it's similar except this API is much cleaner. 3:00 RXJava does include a couple of helpful classes 3:04 that it makes working with observables much easier in certain situations. 3:07 For instance, sometimes you may not care about all three callbacks. 3:12 You may not care about on completed or on error. 3:16 And just want to know when items are emitted. 3:18 If that's the case, you can simply pass an action to the subscribe method. 3:20 The actions call method will be called for each on next called by the Observable. 3:25 By default, Observables emit up to an infinite number of items. 3:31 However, sometimes you may only want to emit a single item. 3:35 For that there's a special observable called single. 3:39 Single is an observable that emits only a single item. 3:42 Because of this, unlike the regular onCompleted, onError, and 3:46 onNext calls, there is a simpler onSuccess or onError callback. 3:49 Next, Rx Java adds additional power by letting 3:53 us manipulate the data through a pipeline before it reaches our observer. 3:58 Here's what I mean by that. 4:02
You need to sign up for Treehouse in order to download course files.Sign up