Skip to content

Commit c92c8a0

Browse files
committed
1.3 Reworked chapter
1 parent f4aad9e commit c92c8a0

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Part 1 - Getting Started/3. Lifetime management.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Subscription subscribe(Observer<? super T> observer)
1515
Subscription subscribe(Subscriber<? super T> subscriber)
1616
```
1717

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.
1919

2020
In the following example, we handle the error of a sequence that failed.
2121

@@ -34,15 +34,15 @@ Output
3434
java.lang.Exception: Oops
3535
```
3636

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.
3838

3939
## Unsubscribing
4040

4141
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:
4242

4343
```java
44-
boolean isUnsubscribed()
45-
void unsubscribe()
44+
boolean isUnsubscribed()
45+
void unsubscribe()
4646
```
4747

4848
Calling `unsubscribe` will stop events from being pushed to your observer.
@@ -66,7 +66,7 @@ Output
6666
1
6767
```
6868

69-
Unsubscribing one observer does not interfere with other observers
69+
Unsubscribing one observer does not interfere with other observers on the same observable.
7070

7171
```java
7272
Subject<Integer, Integer> values = ReplaySubject.create();
@@ -95,7 +95,7 @@ Second: 2
9595

9696
## onError and onCompleted
9797

98-
`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.
9999

100100
```java
101101
Subject<Integer, Integer> values = ReplaySubject.create();
@@ -119,19 +119,23 @@ Completed
119119

120120
## Freeing resources
121121

122-
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.
123123

124124
```java
125125
Subscription s = Subscriptions.create(() -> System.out.println("Clean"));
126126
s.unsubscribe();
127127
```
128-
129128
Output
130129
```
131130
Clean
132131
```
133132

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`.
135139

136140
* `BooleanSubscription`
137141
* `CompositeSubscription`
@@ -144,7 +148,7 @@ The `Subscriptions` factory has more methods, which allow you to compose the use
144148
* `Subscriber`
145149
* `TestSubscriber`
146150

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.
148152

149153
#### Continue reading
150154

0 commit comments

Comments
 (0)