You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Part 1 - Getting Started/3. Lifetime management.md
+14-10Lines changed: 14 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Subscription subscribe(Observer<? super T> observer)
15
15
Subscription subscribe(Subscriber<? super T> subscriber)
16
16
```
17
17
18
-
`subscribe()` consumes events but performs no actions. The overloads that take an `Action1`construct a `Subscriber` with the functions that you provide, or doing nothing where the action is missing.
18
+
`subscribe()` consumes events but performs no actions. The overloads that take one or more `Action` will construct a `Subscriber` with the functions that you provide. Where you don't give an action, the no action is performed.
19
19
20
20
In the following example, we handle the error of a sequence that failed.
21
21
@@ -34,15 +34,15 @@ Output
34
34
java.lang.Exception: Oops
35
35
```
36
36
37
-
If we do not provide a function for error handling, an `OnErrorNotImplementedException` will be *thrown* at the point where `s.onError` is called. It happens here that the producer and the consumer are side-by-side, so you could do a try-catch. However, on a compartmentalised system you won't receive the exception unless you provide a handler to `subscribe`.
37
+
If we do not provide a function for error handling, an `OnErrorNotImplementedException` will be *thrown* at the point where `s.onError` is called, which is the producer's side. It happens here that the producer and the consumer are side-by-side, so we could do a traditional try-catch. However, on a compartmentalised system, the producer and the subscriber very often are in different places. Unless the consumer provides a handle for errors to `subscribe`, they will never know that an error has occured and that the sequence was terminated.
38
38
39
39
## Unsubscribing
40
40
41
41
You can also stop receiving values *before* a sequence terminates. Every `subscribe` overload returns an instance of `Subscription`, which is an interface with 2 methods:
42
42
43
43
```java
44
-
booleanisUnsubscribed()
45
-
voidunsubscribe()
44
+
boolean isUnsubscribed()
45
+
void unsubscribe()
46
46
```
47
47
48
48
Calling `unsubscribe` will stop events from being pushed to your observer.
@@ -66,7 +66,7 @@ Output
66
66
1
67
67
```
68
68
69
-
Unsubscribing one observer does not interfere with other observers
69
+
Unsubscribing one observer does not interfere with other observers on the same observable.
`onError` and `onCompleted` mean the termination of a sequence. An observable that complies with the standard will not emit anything after either of those events. This is something to note both when consuming in Rx and when implementing your own observable.
98
+
`onError` and `onCompleted` mean the termination of a sequence. An observable that complies with the Rx contract will not emit anything after either of those events. This is something to note both when consuming in Rx and when implementing your own observable.
A `Subscription`can be tied to the resources it uses. For that reason, you should remember to dispose of subscriptions. You can create that binding a `Subscription`with the necessary using the [Subscriptions](http://reactivex.io/RxJava/javadoc/rx/subscriptions/Subscriptions.html) factory.
122
+
A `Subscription`is tied to the resources it uses. For that reason, you should remember to dispose of subscriptions. You can create the binding between a `Subscription`and the necessary resources using the [Subscriptions](http://reactivex.io/RxJava/javadoc/rx/subscriptions/Subscriptions.html) factory.
123
123
124
124
```java
125
125
Subscription s =Subscriptions.create(() ->System.out.println("Clean"));
126
126
s.unsubscribe();
127
127
```
128
-
129
128
Output
130
129
```
131
130
Clean
132
131
```
133
132
134
-
The `Subscriptions` factory has more methods, which allow you to compose the use of resources into a single `Subscription`. `Subscription` itself has several implementations.
133
+
`Subscriptions.create` takes an action that will be executed on unsubscription to release the resources. There also are shorthand for common actions when creating a sequence.
134
+
*`Subscriptions.empty()` returns a `Subscription` that does nothing when disposed. This is useful when you are required to return an instance of `Subscription`, but your implementation doesn't actually need to release any resources.
135
+
*`Subscriptions.from(Subscription... subscriptions)` returns a `Subscription` that will dispose of multiple other subscriptions when it is disposed.
136
+
*`Subscriptions.unsubscribed()` returns a `Subscription` that is already disposed of.
137
+
138
+
There are several implementations of `Subscription`.
135
139
136
140
*`BooleanSubscription`
137
141
*`CompositeSubscription`
@@ -144,7 +148,7 @@ The `Subscriptions` factory has more methods, which allow you to compose the use
144
148
*`Subscriber`
145
149
*`TestSubscriber`
146
150
147
-
We will see some more later on.
151
+
We will see more of them later in this book. It is interesting to note that `Subscriber` also implements `Subscription`. This means that we can also use a reference to a `Subscriber` to terminate a subscription.
0 commit comments