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 3 - Taming the sequence/3. Advanced error handling.md
+38Lines changed: 38 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -126,6 +126,44 @@ Here. we've specified that we want to retry once. Our observable fails after two
126
126
127
127
In this example, we have done something naughty: we have made our subscription stateful to demonstrate that the observable is restarted from the source. The observable produced different values the second time around. `retry` does not cache any elements like `replay`, nor would it make sense to do so. It will become apparent why this matters once we discuss the differences between hot and cold observables.
128
128
129
+
### retryWhen
130
+
131
+
`retry` will restart the subscription as soon as the failure happens. If we need more control over this, we can use `retryWhen`.
132
+
133
+
```java
134
+
publicfinalObservable<T> retryWhen(Func1<? super Observable<? extends java.lang.Throwable>,? extends Observable<?>> notificationHandler)
135
+
```
136
+
137
+
The argument to `retryWhen` is a function that takes an observable and returns another. The input observable emits all the errors that `retryWhen` encounters. The resulting observable signals when to retry:
138
+
* if it emits a value, `retryWhen` will retry,
139
+
* if it terminates, `retryWhen` will emit the error and not retry.
140
+
Note that the type of the signaling obserable of the actual values emitted don't matter. The values are discarded and the observable is only used for timing.
141
+
142
+
In the next example, we will construct a retrying policy where we wait 100ms before retrying.
Our source observable emits 2 values and immediately fails. When that happens, the observable of failures inside `retryWhen` emits the error. We delay that emission by 100ms and send it back to signal to retry. `take(2)` guarantees that our signaling observabe will terminate after we receive two errors. That means that `retryWhen` won't retry after 2 failures.
166
+
129
167
## using
130
168
131
169
The `using` operator is for creating observables from resources that need to managed. It guarantees that your resources will be managed regardless of when and how subscriptions are terminated. If you were to just use `create`, you would have to do the managing in the traditional Java paradigm and inject it into Rx. `using` is a more natural way of managing you resources in Rx.
0 commit comments