1 00:00:04,920 --> 00:00:09,320 Hi, my name's Jamie Huson, and I'm an Android developer at Etsy. 2 00:00:09,320 --> 00:00:13,520 In this workshop, we're going to get you started using RX Java. 3 00:00:13,520 --> 00:00:15,120 We'll cover the concepts and 4 00:00:15,120 --> 00:00:20,070 API's by integrating RX Java into a familiar project, a toDoList app. 5 00:00:20,070 --> 00:00:21,080 Let's dive right in. 6 00:00:22,342 --> 00:00:27,610 RX Java is a library of code to handle data using a push and pull mechanism. 7 00:00:27,610 --> 00:00:29,710 Just like a notification on your phone, 8 00:00:29,710 --> 00:00:32,140 some data will be pushed to receiver of that data. 9 00:00:33,240 --> 00:00:37,040 It's a different way of thinking about existing coding patterns you know. 10 00:00:37,040 --> 00:00:39,804 For instance, let's take a look at this array of to-do's. 11 00:00:41,150 --> 00:00:44,838 In your app, you might have a list or array of to-do items. 12 00:00:44,838 --> 00:00:49,421 Usually, you'll [SOUND] iterate over those items using an index starting at zero and 13 00:00:49,421 --> 00:00:50,783 moving to the last item. 14 00:00:50,783 --> 00:00:55,070 Each time you iterate, you'll perform some action with that item. 15 00:00:55,070 --> 00:00:59,100 In our next Java, we think of these items as being pushed to us one at a time. 16 00:01:00,240 --> 00:01:04,520 Over time, each item is emitted and we can handle that item as we did before. 17 00:01:05,680 --> 00:01:09,250 This is very similar to a callback like on a click in Android, 18 00:01:09,250 --> 00:01:13,150 where we receive a notification when an item is tapped on the screen. 19 00:01:13,150 --> 00:01:16,640 In this model our data is being provided as it becomes available. 20 00:01:18,120 --> 00:01:23,169 In our next job the object that emits items is [SOUND] called an Observable. 21 00:01:23,169 --> 00:01:27,649 The callback that receives items [SOUND] is called the Observer. 22 00:01:27,649 --> 00:01:31,504 And Observer subscribes to the observables and starts receiving data and 23 00:01:31,504 --> 00:01:34,250 un-subscribes and the data is no longer received. 24 00:01:35,590 --> 00:01:39,888 Here we can see the Observable emit items [SOUND] and 25 00:01:39,888 --> 00:01:45,119 as they're emitted they're passed along to the Observer. 26 00:01:45,119 --> 00:01:47,350 Here's what that would look like in RX Java code. 27 00:01:48,545 --> 00:01:53,310 Observable.just creates an Observable that will emit each item from our data set. 28 00:01:53,310 --> 00:01:56,034 In this case a todoList [SOUND]. 29 00:01:56,034 --> 00:01:57,970 Now, we've subscribed to the Observable. 30 00:02:00,060 --> 00:02:02,480 An observer's past of the subscribe method. 31 00:02:02,480 --> 00:02:05,180 It's type to the kind of data being emitted. 32 00:02:05,180 --> 00:02:06,830 In this case a Todo object. 33 00:02:08,540 --> 00:02:11,370 Observer contains the callbacks required for subscription. 34 00:02:12,640 --> 00:02:14,560 On next is called for each item emitted. 35 00:02:15,980 --> 00:02:19,590 Uncompleted is called when the Observable has no more items to admit. 36 00:02:21,170 --> 00:02:24,330 Finally, on errors call if an error occurs. 37 00:02:24,330 --> 00:02:29,130 It's important to note that on completed and on error are only ever called once. 38 00:02:29,130 --> 00:02:32,115 And after they are called, on next will not be called again. 39 00:02:32,115 --> 00:02:37,836 [SOUND] Calling subscriber turns back a subscription object. 40 00:02:37,836 --> 00:02:41,495 [SOUND] With our subscription we can later call unsubscribe. 41 00:02:41,495 --> 00:02:44,680 For instance, in on passive our activity in Android. 42 00:02:46,340 --> 00:02:49,370 These are the basics of the RX Java model. 43 00:02:49,370 --> 00:02:53,810 Data is submitted from Observables which are subscribed to by Observers. 44 00:02:53,810 --> 00:02:57,690 Data is pushed to the observables as it becomes available. 45 00:02:57,690 --> 00:03:00,250 If you've worked with loaders on Android before, 46 00:03:00,250 --> 00:03:03,030 it's similar except this API is much cleaner. 47 00:03:04,970 --> 00:03:07,730 RXJava does include a couple of helpful classes 48 00:03:07,730 --> 00:03:12,120 that it makes working with observables much easier in certain situations. 49 00:03:12,120 --> 00:03:16,060 For instance, sometimes you may not care about all three callbacks. 50 00:03:16,060 --> 00:03:18,360 You may not care about on completed or on error. 51 00:03:18,360 --> 00:03:20,840 And just want to know when items are emitted. 52 00:03:20,840 --> 00:03:24,310 If that's the case, you can simply pass an action to the subscribe method. 53 00:03:25,550 --> 00:03:29,520 The actions call method will be called for each on next called by the Observable. 54 00:03:31,260 --> 00:03:35,450 By default, Observables emit up to an infinite number of items. 55 00:03:35,450 --> 00:03:39,240 However, sometimes you may only want to emit a single item. 56 00:03:39,240 --> 00:03:42,720 For that there's a special observable called single. 57 00:03:42,720 --> 00:03:46,050 Single is an observable that emits only a single item. 58 00:03:46,050 --> 00:03:49,420 Because of this, unlike the regular onCompleted, onError, and 59 00:03:49,420 --> 00:03:53,398 onNext calls, there is a simpler onSuccess or onError callback. 60 00:03:53,398 --> 00:03:58,330 Next, Rx Java adds additional power by letting 61 00:03:58,330 --> 00:04:02,790 us manipulate the data through a pipeline before it reaches our observer. 62 00:04:02,790 --> 00:04:03,820 Here's what I mean by that.