- Learning RxJava
- Thomas Nield
- 404字
- 2021-07-02 22:22:54
Observable.defer()
Observable.defer() is a powerful factory due to its ability to create a separate state for each Observer. When using certain Observable factories, you may run into some nuances if your source is stateful and you want to create a separate state for each Observer. Your source Observable may not capture something that has changed about its parameters and send emissions that are obsolete. Here is a simple example: we have an Observable.range() built off two static int properties, start and count.
If you subscribe to this Observable, modify the count, and then subscribe again, you will find that the second Observer does not see this change:
import io.reactivex.Observable;
public class Launcher {
private static int start = 1;
private static int count = 5;
public static void main(String[] args) {
Observable<Integer> source = Observable.range(start,count);
source.subscribe(i -> System.out.println("Observer 1: " + i));
//modify count
count = 10;
source.subscribe(i -> System.out.println("Observer 2: " + i));
}
}
The output is as follows:
Observer 1: 1
Observer 1: 2
Observer 1: 3
Observer 1: 4
Observer 1: 5
Observer 2: 1
Observer 2: 2
Observer 2: 3
Observer 2: 4
Observer 2: 5
To remedy this problem of Observable sources not capturing state changes, you can create a fresh Observable for each subscription. This can be achieved using Observable.defer(), which accepts a lambda instructing how to create an Observable for every subscription. Because this creates a new Observable each time, it will reflect any changes driving its parameters:
import io.reactivex.Observable;
public class Launcher {
private static int start = 1;
private static int count = 5;
public static void main(String[] args) {
Observable<Integer> source = Observable.defer(() ->
Observable.range(start,count));
source.subscribe(i -> System.out.println("Observer 1: " + i));
//modify count
count = 10;
source.subscribe(i -> System.out.println("Observer 2: " + i));
}
}
The output is as follows:
Observer 1: 1
Observer 1: 2
Observer 1: 3
Observer 1: 4
Observer 1: 5
Observer 2: 1
Observer 2: 2
Observer 2: 3
Observer 2: 4
Observer 2: 5
Observer 2: 6
Observer 2: 7
Observer 2: 8
Observer 2: 9
Observer 2: 10
That's better! When your Observable source is not capturing changes to the things driving it, try putting it in Observable.defer(). If your Observable source was implemented naively and behaves brokenly with more than one Observer (for example, it reuses an Iterator that only iterates data once), Observable.defer() provides a quick workaround for this as well.
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- AngularJS Testing Cookbook
- Git Version Control Cookbook
- Vue.js 3.x從入門到精通(視頻教學(xué)版)
- OpenNI Cookbook
- C語言程序設(shè)計學(xué)習(xí)指導(dǎo)與習(xí)題解答
- Go并發(fā)編程實戰(zhàn)
- 飛槳PaddlePaddle深度學(xué)習(xí)實戰(zhàn)
- C程序設(shè)計實踐教程
- 利用Python進(jìn)行數(shù)據(jù)分析
- Python極簡講義:一本書入門數(shù)據(jù)分析與機(jī)器學(xué)習(xí)
- 區(qū)塊鏈技術(shù)進(jìn)階與實戰(zhàn)(第2版)
- 軟件測試綜合技術(shù)
- C語言程序設(shè)計實訓(xùn)教程與水平考試指導(dǎo)
- Getting Started with Python